1

我正在尝试使用 Perl XML::LibXML::reader module 解析 xml 文档。该模块运行良好,我能够解析大部分文档,但是 xml 中的某些部分可能在不同级别上有多个具有相同名称的元素,我不知道如何处理和处理这些元素,

我想要做的是将下面的 strcutre 转换为方便的 perl 数据结构我尝试使用XML::SimpleXML::Twig smplify 子例程(见下文)除了 XML::LibXML 但用它们解析本节是非常慢(比没有它们解析文档慢 20 倍),

 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>
4

1 回答 1

0

我不确定你想要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;

现在,我不确定这是否完全符合您的要求,但更多的是说明如何处理问题。

于 2015-03-03T11:03:26.060 回答