Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Dear Monks,
please help me on the following issue:
given is the following xml-file (in fact this is an excerpt from the huge file but it shows the problem, imho):
<excerpt> <unit> <unitnumber>1</unitnumber> <Name>Entity A</Name> <Boss>Name</Boss> <contactinfo> <Address> <Street>SomeStreet</Street> <Building>1</Building> <zip>00000</zip> <Ort>Town</Ort> </Address> <Telefon> <code>0123</code> <telnumber>456</telnumber> <directcall>78910</directcall> </Telefon> <Fax> <code>0123</code> <telnumber>456</telnumber> <directcall>10987</directcall> </Fax> <email></email> <URL></URL> </contactinfo> <products> <article> <art_code>A3236</art_code> <quantity>554</quantity> </article> <article> <art_code>B9735</art_code> <quantity>386</quantity> </article> <article> <art_code>C1299</art_code> <quantity>322</quantity> </article> <article> <art_code>D1918</art_code> <quantity_small/> </article> <article> <art_code>E0702</art_code> <quantity_small/> </article> <article> <art_code>F1290</art_code> <quantity_small/> </article> </products> </unit> <unit> <unitnumber>2</unitnumber> <Name>Entity B</Name> <Boss>Name</Boss> <contactinfo> <Address> <Street>SomeOtherStreet</Street> <Building>2</Building> <zip>11111</zip> <Ort>City</Ort> </Address> <Telefon> <code>0999</code> <telnumber>456</telnumber> <directcall>78910</directcall> </Telefon> <Fax> <code>0999</code> <telnumber>456</telnumber> <directcall>10987</directcall> </Fax> <email></email> <URL></URL> </contactinfo> <products> <article> <art_code>A1136</art_code> <quantity>1982</quantity> </article> <article> <art_code>B0765</art_code> <quantity>988</quantity> </article> <article> <art_code>C8099</art_code> <quantity>522</quantity> </article> <article> <art_code>D3938</art_code> <quantity_small/> </article> <article> <art_code>E5722</art_code> <quantity_small/> </article> <article> <art_code>F3596</art_code> <quantity_small/> </article> </products> </unit> </excerpt>
I need (among other things) to get the list of paired values "art_code" and "quantity" per unit - e.g. in the following form:
Entity A;A3236;554 Entity A;B9735;386 Entity A;C1299;322 ... Entity B;A1136;1982 etc.
Since I am a novice in Perl I could only make the following crook so far, the output could be after all proceeded with a regex (flush).
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 @units=$treeobjekt->findnodes("//excerpt/unit"); for(my $i=0;$i<@units;$i++) { my $unitname=$units[$i]->findvalue('./Name/text()'); my $art = $units[$i]->findvalue('./products/article'); my $art_chain = join('---', split(/\n/, $art)); print "$unitname;$art_chain\n"; }
I have an additional problem here too. As you see some positions have exact numbers at <quantity> and some others have a tag <quantity_small/>.
I would like to get only the positions where there are exact numbers. I tried to modify the above script in the following way:
my $art_chain; if($units[$i]->findvalue('./products/article/quantity')>0) { $art_chain = join('---', split(/\n/, $art)); }
but it seems to have no effect on the output.
How could I get the paired values in a better way and filter the inexact quantity informations out?
Thank you in advance for your help!

In reply to How to get paired values from the nested XML structure? by vagabonding electron

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (8)
    As of 2015-11-30 12:34 GMT
    Find Nodes?
      Voting Booth?

      What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

      Results (770 votes), past polls