Re: edit an existing XML
Michael Ludwig <[email protected]> Tue, 10 Sep 2013 11:09:21 +0200
| Newsgroups | gmane.comp.lang.perl.xml |
|---|---|
| Message-ID | <20130910090921.GB7668@Dago> |
Lis Maria schrieb am 10.09.2013 um 11:51 (+0530):
> I am new to perl, and the task i have is to read a config file to get
> the values of certain keys and insert this value into an existing xml.
>
> Example :
>
> Config.properties
> ----------------------------
> key1=value1
>
>
> config.xml
> -----------------------------
> <key1></key1>
>
>
> The perl script should be able to insert the value 'value1' into the
> <key1> tag. Can someone suggest the perl module which i can use for
> achieving the same.
Go with XML::LibXML. Note it offers various interfaces for dealing with
XML. Use the DOM interface and go looking for getElementsByTagName to
find the element node (key1), and probably replaceChild like this:
use strict;
use warnings;
use XML::LibXML;
my $parser = XML::LibXML->new;
my $dom = $parser->load_xml(string =>
'<root><elm1>eins</elm1><elm2/></root>');
print $dom->toString(1);
my($elm) = $dom->getElementsByTagName('elm1');
my $txt = $elm->firstChild or die 'element node not found';
my $new = $dom->createTextNode('zwei');
$elm->replaceChild($new, $txt);
print $dom->toString(1);
Michael
_______________________________________________
Perl-XML mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs