Hi All, I have a Huge XML file I need to extract data below with XMLLIB
OUTPUT
VAL1 ,0,0 VAL2,0,0 VEL3,0,0 VAL4,0,0 VAL5 ,490783914,4532
My code
my $parser = XML::LibXML->new(); my $doc = $parser->parse_file($filename); foreach my $book ($doc->findnodes('/mdc/md/mt[text()="VAL1"]') { $val1=$book->findnodes('./r[1]/text ()'); push (@Val,$val1) }
Input XML File
<p> <mdc xmlns:HTML="http://www.w3.org/TR/REC-xml"> <md> <neid> <neun></neun> <nedn>GET_SUB</nedn> <nesw>R4BA06</nesw> </neid> <mi> <mts>20150429141500Z</mts> <gp>900</gp> <mt>VAL1</mt> <mt>VAL2</mt> <mt>VAL3</mt> <mt>VAL4</mt> <mt>VAL5</mt> <mt>VAL6</mt> <mt>VAL7</mt> <mt>VAL8</mt> <mv> <moid>NAME</moid> <r>0</r> <r>0</r> <r>0</r> <r>0</r> <r>490783914</r> <r>0</r> <r>0</r> <r>0</r> </mv> <mv> <moid>NAME1</moid> <r>0</r> <r>0</r> <r>0</r> <r>0</r> <r>4532</r> <r>0</r> <r>0</r> <r>0</r> </mv> </mi> </md> </mdc>

Update

I was not aware of "count(preceding-sibling)" function ,your code just worked fine.
I tried google what are the different option available in LibXML but could not find can u help me on that.
If wanted the output some thing like
VAL1,NAME,0 VAL1,NAME1,0 VAL2,NAME,0 VAL2,NAME1,0

In reply to Extraction of value with XMLLIB by shak

Title:
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, details, 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, summary, 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.