in reply to set parent for some successive elements using XML::Twig
The following should get you started. Note that there is a lack of error checking in the sample code!
use strict; use warnings; use XML::Twig; my $content = do {local $/; <DATA>}; my $twig = XML::Twig->new(pretty_print => 'nice'); $twig->parse($content); my @first = $twig->get_xpath('//one'); my @elts = $twig->get_xpath('//two'); push @elts, $twig->get_xpath('//three'); my $group = XML::Twig::Elt->new ('group'); $group->paste ('after', $first[0]); $_->move ('last_child', $group) for @elts; $content = $twig->sprint; print $content; __DATA__ <root> <one>some text</one> <two>some text</two> <three>some text</three> <four>more text</four> </root>
Prints:
<root> <one>some text</one> <group> <two>some text</two> <three>some text</three> </group> <four>more text</four> </root>
|
|---|