xml - 库 :XML for perl parsing huge xml files through xpath causing core segmentation fault

标签 xml perl xpath

我有一个巨大的格式为 xml 的文件

<XML>
<Application id="1" attr1="some value" attr2="some val"..and many more attr also with nested tags inside application which might contain more attributes
</Application>

<Application id="2"attr1="some value" attr2="some val"..and many more attralso with nested tags inside application which might contain more attributes
</Application>

<Application id="3"attr1="some value" attr2="some val"..and many more attr also with nested tags inside application which might contain more attributes
</Application>

 .... probably 10000 more Application entries
</XML>

每个Application标签只有属性没有内容,但也包含可以有属性的嵌套标签,我需要解析和提取一些属性。我正在使用以下脚本,它在应用程序标签的一小部分上运行良好,但当记录变高时变得非常慢,不幸的是,当我在整个文件甚至一半的文件上运行它时,它会给我一个段错误核心转储文件。

这是我的脚本 非常感谢任何关于如何更好地做到这一点的建议。

最佳答案

我相信您可以通过 XML::LibXML::Reader 来执行此操作,但我对此并不熟悉。下面是使用 XML::Twig 的方法。

我刚刚为您提供了如何获取 Application 元素中的数据的示例。

 #!/usr/bin/perl

use strict;
use warnings;

use XML::Twig;

$filename1 = "exam.xml";

my $parser = XML::Twig->new( twig_handlers => { Application => \&process_application })
                        ->parsefile($filename1);

sub process_application
  { my( $t, $sample)= @_;
    my $hncid    = $sample->att('ID);                     # get an attribute
    my @persons  = $sample->children( 'Person');
    my @aplnamt  = map { $_->att( 'APLN') } @persons;     # that's how you get all attribute values 
    my @students = $sample->findnodes( './Person/Student');
    my @nsschl   = map { $_->att('NS') } @students;
    my @d81      = $sample->descendant('*[@D8CHRG]'); 
    my @d81      = $sample->findnodes('.//*[@D8CHRG]');   # you can use a subset of XPath

    $t->purge;                                           # this is where you free the memory
  }

现在我想到了,您实际上可以使用 XML::Twig::XPath 来获得 XPath 的全部功能,我只是更习惯 XML::Twig 的 native 导航方法。

关于xml - 库 :XML for perl parsing huge xml files through xpath causing core segmentation fault,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17376775/

相关文章:

xml - 使用 JAXB 需要 JSON 字段

perl - 为什么我使用 Perl 的 DBI->connect 会无声无息地失败?

python - lxml XPath-过滤所有文本,包括尾部

perl 只打印数组的最后一行

perl - 从 CGI 迁移到 mod_perl。了解我的、我们的、本地的

xml - 如何在 xmlstarlet 中声明 XPath 命名空间?

html - 找出在 Robot Framework 中有用的 XPath

xml - 如何将 XML 格式的字符串转换为 grails/groovy 中不带空格的单行 XML 字符串?

c# - 使用 LINQ 在 XML 中查询具有相同名称的嵌套元素

java - RestTemplate仅包含两个MessageConverter,并且无法将XML转换为Java对象