in reply to Re: How to modify an XML file using XML::DOM
in thread How to modify an XML file using XML::DOM

Okay, Got your point. I should write the contents into my xml file to see the changes. I tried this and gave me an error : can't call method "getEncoding" on an undefined value ...

MODIFIED CODE : 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"); my $temp1=$address->getAttribute("name"); my $temp2=$address->getAttribute("id"); print("$temp1\t$temp2\n\n"); } } } open(INFILE,">C:/perl/perl_tests/xmlin2.xml"); $doc->printToFile("C:/perl/perl_tests/xmlin2.xml"); $doc->dispose; close(INFILE);

Replies are listed 'Best First'.
Re^3: How to modify an XML file using XML::DOM
by haukex (Archbishop) on Sep 28, 2016 at 06:50 UTC

    Hi ankit.tayal560,

    You don't need the open and close - printToFile does all the handling of writing to the file for you.

    As for the error message, it appears you've come across a bug - or at least a deficiency in the documentation - of XML::DOM. The document needs to have an "XMLDecl" set. You can do this for example via: $doc->setXMLDecl($doc->createXMLDecl('1.0','UTF-8'));

    Hope this helps,
    -- Hauke D

      thanks a lot . worked smoothly (Y)

      sorry but dint understood . I should copy this line of code into my script or into the XML::DOM documentation (i.e. DOM.pm)????

      $doc->setXMLDecl($doc->createXMLDecl('1.0','UTF-8'));