我正在尝试使用Perl XML::LibXML::reader模块解析一个xml文档。这个模块工作得很好,我能够解析大部分文档,但是有一些关于xml的部分可以在不同的级别上具有同名的多个元素,而我不知道如何分配和处理这些元素,
除了XML::LibXML之外,我尝试使用XML::Simple和XML:: smplify子程序(见下文),但是用它们解析这个部分非常慢(x20比解析没有它们的文档慢),
my @conf= eval{($copy->findnodes('criteria'))};
my $t= XML::Twig->new();
my $hash=$t->parse($_->toString)->simplify(forcearray =>1 ]);
$t->purge();有人能建议我如何以更快的方式解析下面的部分,以更快的方式使用XML::LibXML::reader来简化perl数据结构。任何帮助都是值得感谢的
example of such file :
<criteria operator="OR">
<criteria operator="AND"> -> nested element
<criterion test_ref="oval:org.mitre.oval:tst:123" comment="Windows XP is installed"/>
<criterion test_ref="oval:org.mitre.oval:tst:234" comment="file foo.txt exists"/>
<criteria operator="OR"> -> nested element
<criterion test_ref="oval:org.mitre.oval:tst:127" comment="file x.txt exists"/>
<criterion test_ref="oval:org.mitre.oval:tst:127" comment="file y.txt exists"/>
</criteria>
</criteria>
<criteria operator="AND" negate="true"> ->nested element
<criterion test_ref="oval:org.mitre.oval:tst:345" comment="Windows 2003 is installed"/>
<criterion test_ref="oval:org.mitre.oval:tst:456" comment="file fred.txt has a version less than 2"/>
<criterion test_ref="oval:org.mitre.oval:tst:567" negate="true" comment=patch is installed"/>
</criteria>
<criterion test_ref="oval:org.mitre.oval:tst:345" comment="Windows 2003 is installed"/>
</criteria>发布于 2015-03-03 11:03:26
我不确定您是否想要simplify您的XML。看看它,您要寻找的工具是XML::Twig中的小枝处理程序。
例如:
#!/usr/bin/perl
use strict;
use warnings;
use XML::Twig;
use Data::Dumper;
my $xml = q{ <criteria operator="OR">
<criteria operator="AND"> -> nested element
<criterion test_ref="oval:org.mitre.oval:tst:123" comment="Windows XP is installed"/>
<criterion test_ref="oval:org.mitre.oval:tst:234" comment="file foo.txt exists"/>
<criteria operator="OR"> -> nested element
<criterion test_ref="oval:org.mitre.oval:tst:127" comment="file x.txt exists"/>
<criterion test_ref="oval:org.mitre.oval:tst:127" comment="file y.txt exists"/>
</criteria>
</criteria>
<criteria operator="AND" negate="true"> ->nested element
<criterion test_ref="oval:org.mitre.oval:tst:345" comment="Windows 2003 is installed"/>
<criterion test_ref="oval:org.mitre.oval:tst:456" comment="file fred.txt has a version less than 2"/>
<criterion test_ref="oval:org.mitre.oval:tst:567" negate="true" comment="patch is installed"/>
</criteria>
<criterion test_ref="oval:org.mitre.oval:tst:345" comment="Windows 2003 is installed"/>
</criteria> };
my %test_hash;
sub process_criteria {
my ( $twig, $criteria ) = @_;
foreach my $criterion ( $criteria->children('criterion') ) {
my $ref = $criterion->att('test_ref');
my $comment = $criterion->att('comment');
$test_hash{$ref} = $comment;
}
}
my $twig =
XML::Twig->new( twig_handlers => { criteria => \&process_criteria } )
->parse($xml);
print Dumper \%test_hash;现在,我不确定这能实现您想要的结果,但更多的是为了说明如何处理这个问题。
https://stackoverflow.com/questions/20861444
复制相似问题