it should print out 'yes' but it does not.. can someone please give me a pointer? thank youfor my $para ( $root->children('para') ) { if ( $para->first_child_matches('get_this') ) { print $para->first_child('get_this')->att('value'); } }
result when i run thisuse 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'); } }
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |