在Perl中使用巨大的文本节点读取xml的可行方法

前端之家收集整理的这篇文章主要介绍了在Perl中使用巨大的文本节点读取xml的可行方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
遇到包含大文本节点的xml数据文件后,
我找了一些方法来阅读和评估我的数据
处理脚本.

xml文件是用于分子建模的3D坐标文件
应用程序有这种结构(例子):

<?xml version="1.0" encoding="UTF-8"?>
<hoomd_xml version="1.4">
   <configuration>
      <position>
        -0.101000   0.011000  -40.000000
        -0.077000   0.008000  -40.469000
        -0.008000   0.001000  -40.934000
        -0.301000   0.033000  -41.157000
         0.213000  -0.023000  -41.348000
         ...
         ... 300,000 to 500,000 lines may follow  >>
         ...
        -0.140000   0.015000  -42.556000
      </position>

      <next_huge_section_of_the_same_pattern>
        ...
        ...
        ...
      </next_huge_section_of_the_same_pattern>

   </configuration>
</hoomd_xml>

每个xml文件包含几个巨大的文本节点,大小在60MB到100MB之间,具体取决于内容.

我首先使用XML::Simple尝试了naíveapproch,但加载器将永远需要最初解析文件

...
my $data = $xml->XMLin('structure_80mb.xml');
...

并停止“内部错误:巨大的输入查找”,所以这种方法不太实用.

接下来的尝试是使用XML::LibXML进行读取 – 但是在这里,初始加载器会立即出现错误消息“解析器错误:xmlSAX2Characters:巨大的文本节点”.

为了在stackoverflow上写这个主题,我为自己编写了一个q& d解析器并通过它发送文件(在将xx MB xml文件压入标量$xml之后):

...
# read the <position> data from in-memory xml file
my @Coord = xml_parser_hack('position',$xml);
...

它将每行的数据作为数组返回,在几秒钟内完成,如下所示:

sub xml_parser_hack {
 my ($tagname,$xml) = @_;
 return () unless $xml =~ /^</;

 my @Data = ();
 my ($p0,$p1) = (undef,undef);
 $p0 = $+[0] if $xml =~ /^<$tagname[^>]*>[^\r\n]*[r\n]+/msg; # start tag
 $p1 = $-[0] if $xml =~ /^<\/$tagname[^>]*>/msg;             # end tag
 return () unless defined $p0 && defined $p1;
 my @Lines = split /[\r\n]+/,substr $xml,$p0,$p1-$p0;
 for my $line (@Lines) {
    push @Data,[ split /\s+/,$line ];
 }
 return @Data;
}

到目前为止,此工作正常,但当然不能考虑“生产就绪”.

问:如何使用Perl模块读取文件?我会选择哪个模块?

提前致谢

RBO

附录:在阅读了choroba的评论后,我更深入地研究了XML :: LibXML.
我的$reader = XML :: LibXML :: Reader-> new(location =>’structure_80mb.xml’)文件的打开;与我之前的想法相反.如果我尝试访问标记下方的文本节点,则会发生错误

...
while ($reader->read) {
   # bails out in the loop iteration after accessing the <position> tag,# if the position's text node is accessed
   #   --  xmlSAX2Characters: huge text node ---
...

解决方法

我能够使用XML :: LibXML模拟答案.试试这个,让我知道它是否不起作用.我在position元素中创建了一个超过500k行的XML文档,我能够解析它并打印它的内容
use strict;
use warnings;
use XML::LibXML;

my $xml = XML::LibXML->load_xml(location => '/perl/test.xml');
my $nodes = $xml->findnodes('/hoomd_xml/configuration/position');
print $nodes->[0]->textContent . "\n";
print scalar(@{$nodes}) . "\n";

我正在使用findnodes来使用XPath表达式来提取我想要的所有节点. $nodes只是一个数组引用,因此您可以根据文档中实际拥有的节点数循环它.

猜你在找的Perl相关文章