I've written above code to modify the contents of my xml file. but when I tried to create new elements named "item" and "data" it showed me an error : can't locate object method "createElement" via package XML::DOM::ELEMENT. how can I get rid of this problem? any suggestions? it is giving me an output window of Joey 67890 4 and then an error pops up which I just mentioned above
use strict;
use warnings;
use Data::Dumper;
use XML::DOM;
my $parser=new XML::DOM::Parser;
my $doc=$parser->parsefile('C:\perl\perl_tests\xmlin.xml') or die$!;
my $root=$doc->getDocumentElement();
my @address=$root->getElementsByTagName("address");
foreach my $address(@address)
{
if($address->getAttribute("name") eq "tayal")
{
if($address->getAttribute("id")=='70889')
{
$address->setAttribute("name","Joey");
$address->setAttribute("id","67890");
$address->setAttribute("flags","4");
my $temp1=$address->getAttribute("name");
my $temp2=$address->getAttribute("id");
my $temp3=$address->getAttribute("flags");
print("$temp1\n\n");
print("$temp2\n\n");
print("$temp3\n\n");
my $temp_item=$root->createElement("item");
my $temp_data=$root->createElement("data");
my $child1=$address->appendChild($temp_item);
$child1->setAttribute("used","1");
$child1->setAttribute("order","0");
my $g=$child1->getAttribute("used");
my $h=$child1->getAttribute("order");
print("$g\t$h\n");
my $child2=$child1->appendChild($temp_data);
$child2->setAttribute("typeid","4");
my $k=$child2->getAttribute("typeid");
print("$k\n");
}
}
}
$doc->setXMLDecl($doc->createXMLDecl('1.0','UTF-8'));
$doc->printToFile("C:/perl/perl_tests/xmlin2.xml");
$doc->dispose;
XML FILE :
<config logdir="var/log/foo/" debugfile="tmp/foo.debug">
<server name ="sahara" osname ="solaris" osversion="2.6">
<address name="ankit" id="70888"/>
<address name="tayal" id="70889"/>
</server>
<server name="gobi" osname="irix" osversion="6.5">
<address name="anshul" id="70689"/>
</server>
<server name="kalahari" osname="linus" osversion="2.0.34">
<address name="raghu" id="45678"/>
<address name="lucky" id="67895"/>
</server>
</config>