Vinod Shunmugavel
Vinod Shunmugavel

Reputation: 65

How to change the value of a XML tag using Perl

Let us assume that my XML file is like:

<? xml version="1.0" encoding="iso-8859-1" ?>
  <Body>
    <RequiredTag>
      #VALUE#
    </RequiredTag>
  </Body>

How can I change the value of the required tag using Perl?

E.g.:

$XmlHandle->{XML}->{Body}->{RequiredTag} = "RequiredValue";

Upvotes: 3

Views: 456

Answers (1)

mirod
mirod

Reputation: 16161

With XML::Twig::

#!/usr/bin/perl

use strict;
use warnings;

use XML::Twig;

XML::Twig->new( twig_handlers => 
                  { RequiredTag => sub { $_->set_text( 'RequiredValue') } },
                pretty_print => 'indented',
              )
         ->parsefile( 'my_file.xml')
         ->print;

Upvotes: 6

Related Questions