mr_p has asked for the wisdom of the Perl Monks concerning the following question:
Hello Monks,
I am in need of your help again. All the help is apprecicated.
I am tring to traverse an xml get values from it. The program is suppose to parse 'Country' and 'WID'attribute value. The output should look like the below:
USA|1 UK|1 CANADA|1 MEXICO|2 INDIA|2 CHINA|2
but the problem is I am not able to pullout WID value so my out put is incorrect. What am I doing wrong? Please help
#!/usr/bin/perl my $xml_str1 = '<List> <Events WID="1"> <Event Confirmed="Y"> <Country>USA</Country> </Event> <Event Confirmed="Y"> <Country>UK</Country> </Event> <Event Confirmed="Y"> <Country>CANADA</Country> </Event> </Events> <Events WID="2"> <Event Confirmed="Y"> <Country>MEXICO</Country> </Event> <Event Confirmed="Y"> <Country>INDIA</Country> </Event> <Event Confirmed="Y"> <Country>CHINA</Country> </Event> </Events> </List>'; parse_xml($xml_str1); sub parse_xml { use XML::LibXML; no warnings; my $Country = (); my @List = (); my $parser = XML::LibXML->new(); my $doc = $parser->parse_string($_[0]); foreach my $ca_isins ($doc->findnodes('/List//Events/Event')) { my ($titleCountry) = $ca_isins->findnodes('./Country'); $Country = $titleCountry->to_literal . "\n"; my ($widValue) = $ca_isins->findvalue('../../@Wid'); $Country = $titleCountry->to_literal . "|" . $widValue . "\n"; push (@List, $Country); } print "@List"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: XPathing Up level
by Anonymous Monk on Dec 03, 2010 at 02:35 UTC | |
by yuvanbala (Initiate) on Dec 03, 2010 at 06:43 UTC | |
by mr_p (Scribe) on Dec 04, 2010 at 05:52 UTC | |
by ikegami (Patriarch) on Dec 04, 2010 at 08:22 UTC | |
by Anonymous Monk on Dec 03, 2010 at 07:24 UTC | |
|
Re: XPathing Up level
by choroba (Cardinal) on Dec 03, 2010 at 15:06 UTC | |
|
Re: XPathing Up level
by ikegami (Patriarch) on Dec 03, 2010 at 16:50 UTC |