Re: Need help

"Nicolas Mendoza" <[email protected]>
Newsgroups gmane.comp.lang.perl.xml
Organization PVV
Message-ID <op.venj5cbc4q17ch@nicolasm-desktop>
På Mon, 21 Jun 2010 12:05:30 +0200, skrev Chaitanya Yanamadala  
<[email protected]>:

> Hai
>  I have a small query regarding to xml reading and writng..
>
> i have an xml like this
> <a value="5" note=2 >
> <b><c>hello</c></b>
> </a>
>
> now i need to add a new attribute to node a.
>
> can any one help me how to do it..
>
> Chaitanya


There are many ways to do this, but one is:

use XML::TreePP;

my $tpp = XML::TreePP->new();

my $in_xml =<< 'END_XML'
<a value="5" note=2 >
<b><c>hello</c></b>
</a>
END_XML
;

my $tree = $tpp->parse($in_xml);

$tree->{'a'}->{'-new_attribute'} = 'new_value';

print $tpp->write($tree);

$ perl /tmp/lol.pl
<?xml version="1.0" encoding="UTF-8" ?>
<a new_attribute="new_value" value="5">
<b>
<c>hello</c>
</b>
</a>


To do it "properly" you might want to look into XML::LibXML and use DOM  
methods, but for me XML::TreePP does the trick most of the time ;-)

-- Sendt med Operas revolusjonerende e-postprogram:  
http://www.opera.com/mail/
_______________________________________________
Perl-XML mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.