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