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

时间:2013-05-29 11:36:15

标签: xml perl xml-parsing

遇到包含大文本节点的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&amp; 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。 文件my $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 ---
...

2 个答案:

答案 0 :(得分:2)

使用huge解析器选项尝试XML::LibXML

my $doc = XML::LibXML->load_xml(
    location => 'structure_80mb.xml',
    huge     => 1,
);

或者,如果您想使用XML::LibXML::Reader

my $reader = XML::LibXML::Reader->new(
    location => 'structure_80mb.xml',
    huge     => 1,
);

答案 1 :(得分:1)

我能够使用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只是一个数组引用,因此您可以根据文档中实际拥有的节点数来循环显示它。