XML : How to add child element to a node in XMl using XML:Perl

am trying to add child element to a node in XML using XML:Twig. my XML is

   <Install>  <version >  <number>6.0</number>  <build>1037124</build>  <path>path/path>  <kind>kind</kind>  </version>  <version >  <number>7.0</number>  <build>1037124</build>  <path>path</path>  <kind>kind</kind>  </version>  </Install>    

and the output I want is:-

   <Install>  <version >  <number>6.0</number>  <build>1037124</build>  <path>path/path>  <kind>kind</kind>  <patch>patch</patch>  <patchv>patchv</patchv>    </version>  <version >  <number>7.0</number>  <build>1037124</build>  <path>path</path>  <kind>kind</kind>  </version>  </Install>    

I want to add two new node where the version is 6.0

I have tried this code

      my $version = "6.0";          my $file_loc = "data.xml";      my $twig = XML::Twig->new( pretty_print => 'indented_a' )->parsefile( $file_loc );    for my $number ( $twig->findnodes('/Install/version/number') ) {    $number->parent->last_child->insert(                patch => {  version =>'patch' },                patchversion => {  version =>'patchv' },          ) if $number->trimmed_text eq $version;  }    

but the output I am getting is very not what I want. output is:-

  <Install>    <version>      <number>6.0</number>      <build>1037124</build>      <path>path</path>      <kind>        <patch version="patch">      <patchversion version="patchv">kind</patchversion>        </patch>      </kind>    </version>       <version >      <number>7.0</number>      <build>1037124</build>      <path>path</path>      <kind>kind</kind>      </version>      </Install>    

can I achieve this using insert only or I have to use paste?? How to do it in either case ??

No comments:

Post a Comment