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?
In reply to Inserting new elements in an XML doc using XML::Twig by GrandFather
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |