Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Changing XML Tag values in Perl

by anand_perl (Novice)
on Sep 19, 2008 at 08:31 UTC ( #712472=perlquestion: print w/replies, xml ) Need Help??
anand_perl has asked for the wisdom of the Perl Monks concerning the following question:


Hi Monks,

I am trying to modify a XML File using a perl script

I am trying to change the value of a XML tag in that file

The file is a follows

<root>

<test>

<name>file</name>

<href>file.txt</href>

</test>

<test>

<name>file1</name>

<href>file1.txt</href>

</test>

</root>

Now I want to modify the value of the "href" tag inside each "test" node.

For example the value of the "hef" tag in the first "test" node is file.txt.

I want its value to be modified to file.xml

Is this possible??..

I am using the XML::Parser and XML::Simpleobject modules to parse the XML Files

Can anyone help me on this

Thanks in advance....

Replies are listed 'Best First'.
Re: Changing XML Tag values in Perl
by GrandFather (Sage) on Sep 19, 2008 at 09:13 UTC

    Use XML::Twig:

    use strict; use warnings; use XML::Twig; use Data::Dumper; my $xml = <<XML; <?xml version='1.0'?> <root> <test> <name>file</name> <href>file.txt</href> </test> <test> <name>file1</name> <href>file1.txt</href> </test> </root> XML my $twig = XML::Twig->new ( twig_roots => {href => \&editHref,}, twig_print_outside_roots => 1, ); $twig->parse ($xml); $twig->flush; sub editHref { my ($twig, $href) = @_; my $text = $href->text (); $text =~ s/\.txt$/.xml/; $href->set_text ($text); $href->print (); }

    Prints:

    <?xml version='1.0'?> <root> <test> <name>file</name> <href>file.xml</href> </test> <test> <name>file1</name> <href>file1.xml</href> </test> </root> <root><href>file.xml</href><href>file1.xml</href></root>

    Perl reduces RSI - it saves typing
Re: Changing XML Tag values in Perl
by Anonymous Monk on Sep 19, 2008 at 08:54 UTC
    #!/usr/bin/perl -- use strict; use warnings; use XML::Simple; use Data::Dumper; my $xml = q~<?xml version='1.0'?> <root> <test> <name>file</name> <href>file.txt</href> </test> <test> <name>file1</name> <href>file1.txt</href> </test> </root>~; print $xml,$/; my $data = XMLin($xml); print Dumper( $data ); for my $key( keys %{ $data->{test} } ){ $data->{test}{$key}{href} =~ s/\.txt$/.xml/; } print XMLout($data, RootName=>'root', NoAttr=>1,XMLDecl => 1); __END__ <?xml version='1.0'?> <root> <test> <name>file</name> <href>file.txt</href> </test> <test> <name>file1</name> <href>file1.txt</href> </test> </root> $VAR1 = { 'test' => { 'file1' => { 'href' => 'file1.txt' }, 'file' => { 'href' => 'file.txt' } } }; <?xml version='1.0' standalone='yes'?> <root> <test> <name>file</name> <href>file.xml</href> </test> <test> <name>file1</name> <href>file1.xml</href> </test> </root>
Re: Changing XML Tag values in Perl
by Jenda (Abbot) on Sep 21, 2008 at 12:52 UTC

    Using XML::Rules:

    use strict; use warnings; use XML::Rules; my $xml = <<XML; <?xml version='1.0'?> <root> <test> <name>file</name> <href>file.txt</href> </test> <test> <name>file1</name> <href>file1.txt</href> </test> <notest> <name>file1</name> <href>file1.txt</href> </notest> </root> XML my $parser = XML::Rules->new ( style => 'filter', rules => { _default => 'raw', href => sub { my ($tag, $attr, $context) = @_; $attr->{_content} =~ s/\.txt$/.xml/i if $context->[-1] eq +'test'; return $tag => $attr; } } ); $parser->filter($xml);
Re: Changing XML Tag values in Perl
by Anonymous Monk on Sep 19, 2008 at 08:47 UTC
    Where is your code?
      Can any one clear my doubt, how to update the xml element values without changing the xml format if xml elements contains attributes like id, type also.?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://712472]
Approved by mscharrer
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2017-05-26 04:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?