Wiliam
Wiliam

Reputation: 179

Using XML::Twig in Perl, how to add a parent to an existing element?

I have an XML document that looks like this:

<element>
 <sub-element></sub-element>
 <sub-element></sub-element>
 <sub-element id="add_parent"></sub-element>
</element>

and I would like to add a parent to the node having the attribute "add_parent" so it would look like this:

<element>
 <sub-element></sub-element>
 <sub-element></sub-element>
 <new-parent>
  <sub-element id="add_parent"></sub-element>
 </new-parent>
</element>

I am using XML::Twig to select the correct element like this:

#!/usr/bin/perl -w
use warnings;
use XML::Twig;

$t->parsefile ('input.xml');
$v = $t->first_elt('[@id]');

which works fine and I would like to know if it is possible, to encapsulate the select element in a new element?

Upvotes: 2

Views: 135

Answers (1)

mirod
mirod

Reputation: 16161

yes, use the wrap_in method:

$v->wrap_in( 'new-parent');

Upvotes: 2

Related Questions