使用perl XML :: LibXML来如此缓慢地处理XML

使用perl XML :: LibXML来如此缓慢地处理XML,第1张

概述XML文件是这样的: <?xml version="1.0" encoding="UTF-8"?><resource-data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="resource-data.xsd"> <class name="AP"> <attri XML文件是这样的:

<?xml version="1.0" enCoding="UTF-8"?><resource-data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nonamespaceSchemaLocation="resource-data.xsd">  <class name="AP">    <attributes>      <resourceID>00 11 B5 1B 6D 20</resourceID>      <lastModifyTime>20130107091545</lastModifyTime>      <dcTime>20130107093019</dcTime>      <attribute name="NMS_ID" value="DNMS" />      <attribute name="IP_ADDR" value="10.11.141.111" />      <attribute name="LABEL_DEV" value="00 11 B5 1B 6D 20" />    </attributes>        <attributes>      <resourceID>00 11 B5 1B 6D 21</resourceID>      <lastModifyTime>20130107091546</lastModifyTime>      <dcTime>20130107093019</dcTime>      <attribute name="NMS_ID" value="DNMS" />      <attribute name="IP_ADDR" value="10.11.141.112" />      <attribute name="LABEL_DEV" value="00 11 B5 1B 6D 21" />    </attributes>  </class></resource-data>

我的代码:

#!/usr/bin/perluse Encode;use XML::libXML;use Data::Dumper;$parser = new XML::libXML;$struct = $parser->parse_file("d:/AP_201301073100_1.xml");my $file_data = "d:\ap.txt";open IN,">$file_data";$rootel = $struct->getdocumentElement();$elname = $rootel->getname();@kIDs   = $rootel->getElementsByTagname('attributes');foreach $child (@kIDs) {  @atts = $child->getElementsByTagname('attribute');  foreach $at (@atts) {    $va = $at->getAttribute('value');    print IN encode("gbk","$va\t");  }  print IN encode("gbk","\n");}close(IN);

我的问题是,如果XML文件只有80MB,那么程序将非常快,但是当XML文件大得多时,程序可能会非常慢.有人可以帮助我加快速度吗?

解决方法 另一种可能性是使用 XML::LibXML::Reader.它与SAX类似,但使用与XML :: libXML相同的libxml库:

#!/usr/bin/perluse warnings;use strict;use XML::libXML::Reader;my $reader = XML::libXML::Reader->new(location => '1.xml');open my $OUT,'>:enCoding(gbk)','1.out';while ($reader->read) {    attr($reader) if 'attributes' eq $reader->name                     and XML_READER_TYPE_ELEMENT == $reader->nodeType;}sub attr {    my $reader = shift;    my @kIDs;  ATTRIBUTE:    while ($reader->read) {        my $name = $reader->name;        last ATTRIBUTE if 'attributes' eq $name;        next ATTRIBUTE if XML_READER_TYPE_END_ELEMENT == $reader->nodeType;        push @kIDs,$reader->getAttribute('value')            if 'attribute' eq $name;    }    print {$OUT} join("\t",@kIDs),"\n";}
总结

以上是内存溢出为你收集整理的使用perl XML :: LibXML来如此缓慢地处理XML全部内容,希望文章能够帮你解决使用perl XML :: LibXML来如此缓慢地处理XML所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1265806.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存