Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: How to get paired values from the nested XML structure?

by vagabonding electron (Hermit)
on Nov 01, 2011 at 19:20 UTC ( #935189=note: print w/ replies, xml ) Need Help??


in reply to How to get paired values from the nested XML structure?

Dear All,

it seems to run now in the following setting:

use strict; use warnings; use XML::LibXML; my $filename = "Test.xml"; my $my_object = XML::LibXML->new(); my $treeobjekt = $my_object->parse_file($filename); my $root = $treeobjekt->getDocumentElement; my @articles = $treeobjekt->findnodes('//article'); for(my $j=0;$j<@articles;$j++) { my $unitname = $articles[$j]->parentNode->parentNode->findvalu +e('./Name/text()'); my $article = $articles[$j]->findvalue('./art_code/text()'); my $amount = $articles[$j]->findvalue('./quantity/text()')//"0 +"; print "$unitname;$article;$amount\n"; }
prints:
Entity A;A3236;554 Entity A;B9735;386 Entity A;C1299;322 Entity A;D1918; Entity A;E0702; Entity A;F1290; Entity B;A1136;1982 Entity B;B0765;988 Entity B;C8099;522 Entity B;D3938; Entity B;E5722; Entity B;F3596;
which can be easily processed further.
I still do not understand why the condition
findvalue('./quantity/text()')//"0";
does not work.
I would be very glad if you would improve this code.
Many thanks!
VE


Comment on Re: How to get paired values from the nested XML structure?
Select or Download Code
Re^2: How to get paired values from the nested XML structure?
by choroba (Abbot) on Nov 01, 2011 at 22:00 UTC
    You can use Data::Dumper to see what your variables contain. For articles without quantity, $amount is the empty string q(), which means it is defined. If you replace // with ||, you will get the desired behaviour.
      Thank you choroba it works this way! Again I've learned on PM!
      ... now trying this "in real life" with the complete file.
      Thanks again.
      VE

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://935189]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2014-10-21 05:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (96 votes), past polls