Selvakumar has asked for the wisdom of the Perl Monks concerning the following question:
1. I have added child element using the below code and i need to add again child or descendants to the root tag. how can i do this?
2. First step i am changing the "indd_document" element to "docuemnt". i have a doubt here the xml::twig now recognize "docuemnt" or "indd_docuemnt" for my second twig_roots for update_header. Please suggest
INPUT:use strict; use warnings; use XML::Parser; use XML::Twig; my $twig=XML::Twig->new( twig_roots=>{ i +ndd_document=>\&update_root, 'document/header'=>\&update_header +, }, ); $twig->parsefile( $xmlfile); open(FH,">output.xml") or die "cannot open output.xml: $!"; my $temp = $twig->toString; print FH $temp; close (FH); $twig->purge; } sub update_root { my ($t, $root) = @_; $root->insert_new_elt(first_child => header => {type=>"layout"}); $root->set_tag( 'document'); } sub update_header { my ($t, $header)=@_; $header->insert_new_elt(first_child => headerfield => {type=>"layout"} +); }
OUTPUT:<?xml version="1.0"?> <indd_document> <para> </para> </indd_document>
<?xml version="1.0"?> <indd_document> <header> <headerchild name=""/> <headerchild name=""/> </header> <para> </para> </indd_document>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: xml::child add child and descendants
by mickep76 (Beadle) on Oct 08, 2009 at 13:46 UTC | |
|
Re: xml::child add child and descendants
by mirod (Canon) on Oct 08, 2009 at 13:14 UTC | |
by Selvakumar (Scribe) on Oct 08, 2009 at 13:52 UTC | |
by mirod (Canon) on Oct 08, 2009 at 15:07 UTC |