parsing nested oval file with XML::Twig

468 Views Asked by At

I have the following OVAL definintion and want to parse it and store it on specific perl object like a array ref and then accessing it as object

for example to access the first comment attrbitue of the below 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}

I tried to parse it with XML::Twig handlers and i get the criteria element but i didn't know how to treat a nested criteria elements in order to build my perl object/data structure

any idea how can i achieve this with XML::Twig and perl ?

1

There are 1 best solutions below

1
On

You can try to select <criteria> elements using twig_handlers() and inside them the <criterion> elements with children(), and save either attributes in a hash that is pushed into an array ref variable.

#!/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'}

With your fixed xml file, $arr will be like:

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'