我有以下OVAL定义,希望解析它并将其存储在特定的perl对象上,如数组ref,然后将其作为对象进行访问
例如,要访问以下xml的第一个注释属性:
<criteria operator="OR">
<criteria operator="AND">
<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>
<criteria operator="AND" negate="true">
<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>
it will be somthing like this:
$arr->[0]->[1]->{oval-org-mitre-oval-tst-123}->{comment}我尝试使用XML::Twig处理程序对其进行解析,得到了criteria元素,但我不知道如何处理嵌套的criteria元素来构建perl对象/数据结构
你知道如何使用XML::Twig和perl来实现这一点吗?
发布于 2013-12-24 19:33:47
您可以尝试使用twig_handlers()选择<criteria>元素,并在其中使用children()选择<criterion>元素,然后将这两个属性保存到一个推入数组ref变量的散列中。
#!/usr/bin/env perl
use warnings;
use strict;
use XML::Twig;
my $arr = [];
XML::Twig->new(
twig_handlers => {
'criteria' => sub {
my %hash;
for my $child ( $_->children( 'criterion' ) ) {
$hash{
do { (my $k = $child->att('test_ref')) =~ tr/:./-/; $k } } =
$child->att( 'comment' );
}
push @$arr, { %hash };
},
},
)->parsefile(shift);
## print $arr->[0]
## or
## print $arr->[0]{'oval-org-mitre-oval-tst-123'}使用固定的xml文件,$arr将如下所示:
0 ARRAY(0x25f9200)
0 HASH(0x3613958)
'oval-org-mitre-oval-tst-123' => 'Windows XP is installed'
'oval-org-mitre-oval-tst-234' => 'file foo.txt exists'
1 HASH(0x360e068)
'oval-org-mitre-oval-tst-345' => 'Windows 2003 is installed'
'oval-org-mitre-oval-tst-456' => 'file fred.txt has a version less than 2'
'oval-org-mitre-oval-tst-567' => 'patch is installed'
2 HASH(0x3613430)
'oval-org-mitre-oval-tst-345' => 'Windows 2003 is installed'https://stackoverflow.com/questions/20759904
复制相似问题