views:

65

answers:

3

Hi All,

I am having a XML file as shown below,

<message1>
<val1>100</val1>
<val2>200</val2>
<val3>300</val3>
<val4>400</val4>
</message1>

<message2>
<val1>100</val1>
<val2>200</val2>
<val3>300</val3>
<val4>400</val4>
</message2>

I have to parse the values (val) and i could not use XML::Simple module. The parsing should be started from <message1> and i have to put the values in an array till </message1> and then i have to repeat this for <message2> till </message2>.

Pictorially it is like

<message1>
   ----100
   ----200
   ----300
   ----400
</message1>

<message2>
   ----100
   ----200
   ----300
   ----400
</message2>

Can any one help me .. I am struggling a lot

Thanks

Senthil kumar

A: 

you could potentially use XPath

MasterMax1313
This answer could potentially use some effort.
daxim
+1  A: 

Assuming your input is completely regular as you show, the following should work. But you are far better off getting a real XML parser to work, by wrapping a root element around all your content or by parsing each message separately.

use strict;
use warnings;

my %data;
while (<>) {
    # skip blank lines
    next unless /\S/;

    my ($tag) = /^<(.*)>$/
        or warn("expected tag, got $_ "), next;
    $data{$tag} ||= [];

    while (<>) {
        last if /^<\/\Q$tag\E>$/;

        my (undef, $value) = /^<val(\d+)>(.*)<\/val\1>$/
            or warn("expected val, got $_ "), next;
        push @{ $data{$tag} }, $value;
    }
}

use Data::Dumper;
print Dumper \%data;
ysth
+2  A: 

Since we're back in 1999, I think I would forget about strict and warnings, use symbolic references and string eval, and be done with it:

#!/usr/bin/perl

while( <DATA>)
  { s{<(message\d)>}{\@$1=(}; # @message1=(
    s{<val\d>}{};             #
    s{<\/val\d>}{,};          #                ,
    s{</message\d>}{);};      #                 );
    $s.=$_;
  };

eval $s;

$,= ", "; $\= "\n";
foreach (1..2) { print "\@message$_: ", @{"message$_"}; }



__DATA__
<message1>
<val1>100</val1>
<val2>200</val2>
<val3>300</val3>
<val4>400</val4>
</message1>

<message2>
<val1>100</val1>
<val2>200</val2>
<val3>300</val3>
<val4>400</val4>
</message2>

(in case that's not clear: that's a joke! As they say "Have you tried using an XML parser instead?")

mirod
No YAML in 1999, sorry.
ysth
@ysth: you're right, I removed it. I also removed the my declaration, sorry, good habits die hard.
mirod