Perl SAX 方式解析 XML
发布时间:2020-12-15 21:08:56 所属栏目:大数据 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 use XML::Parser;$currentLine = 0;$parser = new XML::Parser(Handlers = {Start = start_handler,End = end_handler,Char = char_handler,Proc =
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 use XML::Parser; $currentLine = 0; $parser = new XML::Parser(Handlers => {Start => &;start_handler,End => &;end_handler,Char => &;char_handler,Proc => &;proc_handler,XMLDecl => &;XMLDecl_handler,Final => &;final_handler}); $parser->parsefile("yourXML.xml"); sub XMLDecl_handler { $xmlString[$currentLine++] = "<?xml version="$_[1]"?>"; } sub start_handler { $xmlString[$currentLine] = $indent . "<$_[1]"; for ($i = 2; $i <= $#_ - 1; $i += 2){ $xmlString[$currentLine] .= " " . $_[$i] . "="". $_[$i + 1] . """; } $xmlString[$currentLine++] .= ">"; $indent .= " "; } sub end_handler { $indent = substr($indent,length($indent) - 4); $xmlString[$currentLine++] = $indent . "</$_[1]>"; } sub char_handler { if($_[1] =~ /[^ ntr]/g) { $xmlString[$currentLine++] = $indent . "$_[1]"; } } sub proc_handler { $xmlString[$currentLine++] = "<?$_[1] $_[2]?>"; } sub final_handler { for ($i = 0; $i < $currentLine; $i++){ print $xmlString[$i] . "n"; } } 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |