Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

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

by vagabonding electron (Chaplain)
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"; }
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
does not work.
I would be very glad if you would improve this code.
Many thanks!

Replies are listed 'Best First'.
Re^2: How to get paired values from the nested XML structure?
by choroba (Bishop) 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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://935189]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2018-06-24 09:40 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.