开发者

help in parsing

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 h开发者_开发问答elp me .. I am struggling a lot

Thanks

Senthil kumar


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?")


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜