Ok very very frustrated xml learner here(due to my lack of will power and ability).
So I decide to write a dummy XML file to give myself better understanding of what XML::Twig doc is trying to say.

I learn better by doing it visually so I was typing it away while looking up XML::Twig doc.
I am doing ok so far(I also had help from other perl monks from here already.. but still not getting it!!!!!
Anyway, below is the code which gives me fairly decent idea but my last portion is not working as I expected.. I wanted to use 'first_child_matches' to find the element and print it's value ..

I also hope my dumb example would give the other newbie some clue as well

for my $para ( $root->children('para') ) { if ( $para->first_child_matches('get_this') ) { print $para->first_child('get_this')->att('value'); } }
it should print out 'yes' but it does not.. can someone please give me a pointer? thank you
use strict; use warnings; use XML::Twig; my $xml = <<XML; <root> <para id="000">0000000000000000000000000000000000000000000<yahoo>XXXXX +XX </yahoo> <get_this value="yes" /> <get_this_two>ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ</get_this_two> </para> <para id="111">1111111111111111111111111111111111111111111</para> <para id="222">2222222222222222222222222222222222222222222</para> <para id="333">3333333333333333333333333333333333333333333</para> <para id="444">4444444444444444444444444444444444444444444</para> <para id="555">5555555555555555555555555555555555555555555</para> <para_type type="para type 3rd" active="true"> <para_cat type="category 3" /> </para_type> <para_type type="para type 2nd" active="false"> <para_cat type="category 2" /> </para_type> <para id="XXX" value="XXX_para" > XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX <para_2nd value="PPPP" /> <para_3rd> <para_3rd_name value="para type 3rd" active="true"> <para_4th value="empty" active="false"/> </para_3rd_name> </para_3rd> <para_4th value="on" eff="no">does this work</para_4th> </para> </root> XML my $tmp = XML::Twig->new(); $tmp->parse ($xml); my $root = $tmp->root; print "Here is XML file \n" . $xml . "\n"; print ">>>>>>>>>> children of when 'para' is root<<<<<<<<<<<<<\n"; print $_->trimmed_text (), "\n" for $root->children ('para'); print ">>>>>>>>>> first_child of when 'para' is root <<<<<<<<<<<<<\n"; print $_->text (), "\n" for $root->first_child ('para'); print ">>>>>>>>>> last_child of when 'para' is root <<<<<<<<<<<<<\n"; print $_->text (), "\n" for $root->last_child('para'); print ">>>>>>>>>> children of when 'para_4th' is root<<<<<<<<<<<<<<<\n +"; print $_->text (), "\n" for $root->last_child('para')->first_child('pa +ra_4th'); print ">>>>>>>>>> find att 'eff' value from para_4th <<<<<<<<<<<<<\n"; print $_, "\n" for $root->last_child('para')->first_child('para_4th' +)->att('eff'); print ">>>>>>>>>> children of when 'para' is root find value of id<<<< +<<<<<<<<<<<<<<\n"; #print $_, "\n" for $root->children('para')->att('id'); for my $para ( $root->children('para') ) { print $para->att('id') . "\n"; } for my $para ( $root->children('para') ) { if ( $para->first_child_matches('get_this') ) { print $para->first_child('get_this')->att('value'); } }
result when i run this
Here is XML file <root> <para id="000">0000000000000000000000000000000000000000000<yahoo>XXXXX +XX </yahoo> <get_this value="yes" /> <get_this_two>ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ</get_this_two> </para> <para id="111">1111111111111111111111111111111111111111111</para> <para id="222">2222222222222222222222222222222222222222222</para> <para id="333">3333333333333333333333333333333333333333333</para> <para id="444">4444444444444444444444444444444444444444444</para> <para id="555">5555555555555555555555555555555555555555555</para> <para_type type="para type 3rd" active="true"> <para_cat type="category 3" /> </para_type> <para_type type="para type 2nd" active="false"> <para_cat type="category 2" /> </para_type> <para id="XXX" value="XXX_para" > XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX <para_2nd value="PPPP" /> <para_3rd> <para_3rd_name value="para type 3rd" active="true"> <para_4th value="empty" active="false"/> </para_3rd_name> </para_3rd> <para_4th value="on" eff="no">does this work</para_4th> </para> </root> >>>>>>>>>> children of when 'para' is root<<<<<<<<<<<<< 0000000000000000000000000000000000000000000XXXXXXX ZZZZZZZZZZZZZZZZZZZ +ZZZZZZZZZZZ 1111111111111111111111111111111111111111111 2222222222222222222222222222222222222222222 3333333333333333333333333333333333333333333 4444444444444444444444444444444444444444444 5555555555555555555555555555555555555555555 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX does this work >>>>>>>>>> first_child of when 'para' is root <<<<<<<<<<<<< 0000000000000000000000000000000000000000000XXXXXXX ZZZZZZZZZZZZZZZZZZZ +ZZZZZZZZZZZ >>>>>>>>>> last_child of when 'para' is root <<<<<<<<<<<<< XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX does this work >>>>>>>>>> children of when 'para_4th' is root<<<<<<<<<<<<<<< does this work >>>>>>>>>> find att 'eff' value from para_4th <<<<<<<<<<<<< no >>>>>>>>>> children of when 'para' is root find value of id<<<<<<<<<<< +<<<<<<< 000 111 222 333 444 555 XXX

In reply to XML::Twig first_child_matches question by convenientstore

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.