Hello Monks, I have an xml file formatted in a not so neat way and I need to extract all of the information I can from it. Here's an example.

<IRETURNVALUE> <INSTANCE CLASSNAME="junk"><PROPERTY NAME="ID"><DISPLAY TYPE="string"> +A0</DISPLAY><VALUE TYPE="string">a0</VALUE></PROPERTY><PROPERTY NAME= +"state"><DISPLAY TYPE="string">Active</DISPLAY><VALUE TYPE="string">a +ctive</VALUE></PROPERTY>...</INSTANCE> <INSTANCE CLASSNAME="junk"><PROPERTY NAME="ID"><DISPLAY TYPE="string"> +A1</DISPLAY><VALUE TYPE="string">a1</VALUE></PROPERTY><PROPERTY NAME= +"state"><DISPLAY TYPE="string">Active</DISPLAY><VALUE TYPE="string">a +ctive</VALUE></PROPERTY>...</INSTANCE> </IRETURNVALUE>

Each of these lines has about 8 or 9 different properties. What I need to do is to store each of these lines in a %data hash using the ID as the key. My preference is to use XML::Rules to do this, but I'm open to any suggestions using XML::Parser or XML::Twig. So far my pseudocode looks something like this:

#!/usr/bin/perl -w use strict; use warnings; use XML::Rules; my %data; my @rules = ( my $id; #$id will be the _content of DISPLAY after the Property Name my $var; #$var will be the _content of DISPLAY PROPERTY => sub { if ($_[1]{NAME} eq "id"){ $data{$id} = {}; } else ($data{$id} = {$_[1]{NAME} => $var}) } }, DISPLAY => sub {#some code to initialize $var}; my $xr = XML::Rules->new( rules => \@rules, stripspaces => 2 ); $xr->parse(<DATA>);

This is more or less the direction I was taking. Hope I was clear enough.

Edit : Solved, not the most elegant way to solve it but it does the job.

#!/usr/bin/perl -w use strict; use warnings; use vars qw/ %options /; use Getopt::Std; use XML::Rules; use Data::Dumper; #..... # Code to treat file #..... my %data; my $temp; my $id; my $trigger = 0; my @rules = ( PROPERTY => sub { if ($_[1]{NAME} eq "id"){ $data{$id} = {}; #Creates empty hash using the id as t +he key $trigger = 1; } elsif ($_[1]{NAME} eq "class"){ #class is the last tag bef +ore the new line $data{$id} -> {@{$_[1]}{NAME}} = $temp; $trigger = 0; #this resets the trigger to let the scri +pt know that the next property is } #going to be the id else { $data{$id} -> {@{$_[1]}{NAME}} = $temp; } }, DISPLAY => sub { if ($trigger == 0){ $id = @{$_[1]}{_content}; } else { $temp = @{$_[1]}{_content}; } }, ); my $xr = XML::Rules->new( rules => \@rules, stripspaces => 2 ); $xr->parsefile($File); print Dumper(\%data);

In reply to (Solved) : XML::Rules using parent/child to parse through XML by Falantar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.