in reply to XML::Twig - parsing an XML doc to separate files : Maintaining 'absolute' path
It seems to me that for each element of the appropriate level, you want to output the entire tree, including the element, then delete the element so when you get to the next element you get a "clean" tree.
The code below gives you the output you want:
#!/usr/local/bin/perl -w use strict; use XML::Twig; my $xml_twig = XML::Twig->new( pretty_print => 'indented', NoLWP => 1, discard_spaces => 1, twig_handlers => { 'level(1)' => \&dump_partial_tree } ); $xml_twig->parse ( \*DATA ) || die "\nError parsing data $@\n"; sub dump_partial_tree { my( $xml_twig, $element)= @_; print "\n\nName : " . $element->name . "\n"; $xml_twig->print; print "\n"; $element->delete; } __DATA__ <myconfig_data> <level1> <tag>hello1</tag> </level1> <level2> <tag>hello2</tag> </level2> <level3> <tag>hello3</tag> </level3> </myconfig_data>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: XML::Twig - parsing an XML doc to separate files : Maintaining 'absolute' path
by Anonymous Monk on Feb 22, 2006 at 16:22 UTC | |
by mirod (Canon) on Feb 22, 2006 at 16:48 UTC |