Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello again,

Everyone has been so helpful I thought I would ask one last question regarding XML::Twig.

I'm using the following code

#!/perl/bin/perl -w use strict; use XML::Twig; my $twig= new XML::Twig; open( XML_OUT, ">output.xml") or die $!; $twig->parsefile( "RoamAboutAP_173.253.52.82.xml"); my $root= $twig->root; my @ds= $root->children; foreach my $attr (@ds) { my $look = $attr->parent('rrd')->text; print $look; my $blg= sprintf( "TESTING PLACEMENT"); my $eblg= new XML::Twig::Elt( 'Average', $blg); $eblg->paste( 'first_child', $attr); } # $twig->set_pretty_print('indented'); $twig->print( \*XML_OUT); close XML_OUT;

My XML file looks like this:

- <rrd> <version>0001</version> <step>300</step> - <!-- Seconds --> <lastupdate>1042244888</lastupdate> - <!-- 2003-01-10 19:28:08 Eastern Standard Time --> - <ds> <name>ifOutOctets1</name> <type>COUNTER</type> <minimal_heartbeat>600</minimal_heartbeat> <min>NaN</min> <max>NaN</max> - <!-- PDP Status --> <last_ds>1636141175</last_ds> <value>6.2416000000e+002</value> <unknown_sec>0</unknown_sec> </ds> - <!-- Round Robin Archives --> - <rra> <cf>AVERAGE</cf> <pdp_per_row>1</pdp_per_row> - <!-- 300 seconds --> <xff>5.0000000000e-001</xff> - <cdp_prep> - <ds> <value>NaN</value> <unknown_datapoints>0</unknown_datapoints> </ds> - <ds> <value>NaN</value> <unknown_datapoints>0</unknown_datapoints> </ds> </cdp_prep> - <database> - <!-- 2003-01-08 17:30:00 Eastern Standard Time / 1042065000 --> - <row> <v>NaN</v> <v>NaN</v> <v>NaN</v> <v>NaN</v> </row> - <!-- 2003-01-08 17:35:00 Eastern Standard Time / 1042065300 --> - <row> <v>6.3173066667e+001</v> <v>2.2418133333e+001</v> <v>3.7560151111e+002</v> <v>1.7984751111e+002</v> </row> - <!-- 2003-01-10 19:25:00 Eastern Standard Time / 1042244700 --> - <row> <v>9.5548400000e+001</v> <v>3.1534622222e+001</v> <v>4.0781324444e+002</v> <v>1.9573493333e+002</v> </row> </database> </rra> </rrd>
What I want to do is search for a specidifc tag, say maybe <ds> or the <v> tag.

I am able to sucessfully look at the root tag, but how can I look at the children of the root node in my foreach loop?

I'll continue reading the perldoc
Thanks!

Replies are listed 'Best First'.
Re: XML Twig parse
by mirod (Canon) on Jan 20, 2003 at 20:39 UTC

    Instead of using $root->children just use $root->children( 'ds') which will give you only the ds children, in the document order.

    All navigation methods (children, first_child, next_sibling, descendants...) accept an extra argument, a condition used to filter results. See the module doc (which I admit is pretty huge and hard to navigate) for more info.

Re: XML Twig parse
by boo_radley (Parson) on Jan 20, 2003 at 20:41 UTC

    What I want to do is search for a specidifc tag, say maybe <ds> or the <v> tag.
    you can specify search criteria in the call to the children method :
    my @ds= $root->children("ds");# get only ds children
    my @ds= $root->children('ds[@name="foo"]');# get only ds children with a name attribute of foo
    and so on.

    update Damn module authors jumping in :-) Hey mirod, you should expound on conditions in the docs a bit more, a lot of people get tripped up on them.

      I will write more about conditions once I am happy with them ;--( At the moment they are processed by a very poor regex-based parser. I need to write a real XPath parser, probably using Parse::Yapp. Then the doc will be written... it will be the XPath spec! Except that this will be "navigational XPath", and that I will also have to properly identify the subset of XPath that can be used for twig_roots (all we know about an element is its ancestors, and maybe their attributes) or twig_handlers (we know everything that happened before the end tag of the element),so I know when to refuse an XPath expression if it is in the wrong context.

      As for now, the doc for the development version is now generated through perldoc and is a little better x-linked, so I will add links to the description of the conditions from all navigation functions.

        Thanks Mirod

        One last question, what is the syntax to get the children of children?

        I.E.

        The children of rra and or their children cdp_prep or database
        Thanks