mohan2monks has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks,
I am working on a old code which uses XMLSimple to parse XML data.
I have a data set in which i need to relate the tags by occurance like Phone tag if followed by preceding Name tag then it is related to it.
I can't do this with XMLSimple as the data structure it returns creates the keys of Name tag and Phone tag thereby grouping the data related to them.
Please see this example
I have a data set in which i need to relate the tags by occurance like Phone tag if followed by preceding Name tag then it is related to it.
I can't do this with XMLSimple as the data structure it returns creates the keys of Name tag and Phone tag thereby grouping the data related to them.
Please see this example
From this i can't make out which phone number is associated with which name, Please help.use XML::Simple; use Data::Dumper; my $data=qq(<Data><Name><Lname>xyz</Lname><Fname>abcd</Fname></Name><P +hone><Mobile>123456789</Mobile><Home>55656556</Home></Phone><Phone><M +obile>123456789</Mobile><Home>55656556</Home></Phone><Name><Lname>UTV +</Lname><Fname>EFGH</Fname></Name><Phone><Mobile>123456789</Mobile><H +ome>55656556</Home></Phone></Data>); my $ref=XMLin($data); print Dumper($ref); which shows output $VAR1 = { 'Phone' => [ { 'Home' => '55656556', 'Mobile' => '123456789' }, { 'Home' => '55656556', 'Mobile' => '123456789' }, { 'Home' => '55656556', 'Mobile' => '123456789' } ], 'Name' => [ { 'Lname' => 'xyz', 'Fname' => 'abcd' }, { 'Lname' => 'UTV', 'Fname' => 'EFGH' } ] };
Out of sheer desperation may be i can use this
#on above $data i can split up the above string with each #Name tag followed by Phone tags associated with this my @namephn=map{m/<Name>.*?<\/Phone>/g } $data; print join "\n",@namephn; #---------- Output ------------- <Name><Lname>xyz</Lname><Fname>abcd</Fname></Name><Phone><Mobile>12345 +6789</Mobile><Home>55656556</Home></Phone> <Name><Lname>UTV</Lname><Fname>EFGH</Fname></Name><Phone><Mobile>12345 +6789</Mobile><Home>55656556</Home></Phone>
But how do i make that lazy ? operator match more than one phone tag till the next Name tag :-(
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Relating data parsed with XMLSimple
by Jenda (Abbot) on Mar 30, 2010 at 11:42 UTC | |
by mohan2monks (Beadle) on Mar 30, 2010 at 12:00 UTC | |
Re: Relating data parsed with XMLSimple
by Anonymous Monk on Mar 30, 2010 at 10:17 UTC | |
by mohan2monks (Beadle) on Mar 30, 2010 at 11:15 UTC | |
by Anonymous Monk on Mar 30, 2010 at 11:19 UTC | |
by mohan2monks (Beadle) on Mar 30, 2010 at 11:27 UTC | |
by Anonymous Monk on Mar 31, 2010 at 01:36 UTC | |
Re: Relating data parsed with XMLSimple
by rovf (Priest) on Mar 30, 2010 at 11:09 UTC | |
by mohan2monks (Beadle) on Mar 30, 2010 at 11:32 UTC | |
by Jenda (Abbot) on Mar 30, 2010 at 11:49 UTC |
Back to
Seekers of Perl Wisdom