GrandFather has asked for the wisdom of the Perl Monks concerning the following question:
I have a list of key words that I wish to insert elements for into an existing XML document. What I have so far is:
use strict; use warnings; use XML::Twig; my @keywords = qw(this that the other); my $twig = XML::Twig->new ( pretty_print => 'indented', discard_spaces => 1, keep_encoding => 1, ); $twig->parse (do {local $/; <DATA>}); my $topic = $twig->root (); my @newElts; push @newElts, [keyword => {translate => 'true'}, $_] for @keywords; my $keywordsElt = XML::Twig::Elt->new (keywords => {}, @newElts); $keywordsElt->paste (first_child => $topic); print $twig->sprint (); __DATA__ <?xml version="1.0" encoding="UTF-8"?> <topic> <body> </body> </topic>
which prints:
<?xml version="1.0" encoding="UTF-8"?> <topic> <keywords>ARRAY(0x1fa77a8)ARRAY(0x1fa77e4)ARRAY(0x1fa7850)ARRAY(0x1f +a788c)</keywords> <body></body> </topic>
but what I'd like is:
<?xml version="1.0" encoding="UTF-8"?> <topic> <keywords> <keyword translate='true'>this</keyword> <keyword translate='true'>that</keyword> <keyword translate='true'>the</keyword> <keyword translate='true'>other</keyword> </keywords> <body></body> </topic>
What am I missing?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Inserting new elements in an XML doc using XML::Twig
by ysth (Canon) on Feb 12, 2007 at 06:17 UTC | |
by GrandFather (Saint) on Feb 12, 2007 at 10:03 UTC | |
by Herkum (Parson) on Feb 12, 2007 at 19:05 UTC | |
by mirod (Canon) on Feb 12, 2007 at 19:23 UTC | |
by GrandFather (Saint) on Feb 12, 2007 at 20:09 UTC | |
| |
by Tanktalus (Canon) on Feb 12, 2007 at 20:07 UTC | |
by Withigo (Friar) on Feb 22, 2007 at 02:02 UTC |