如何使用Perl更改XML标记的值

时间:2022-11-27 16:55:57

Let us assume that my XML file is like:

我们假设我的XML文件是这样的:

<? 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?

如何使用Perl更改所需标记的值?

E.g.:

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

1 个解决方案

#1


6  

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;

#1


6  

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;