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

Re: How to modify an XML file using XML::DOM

by afoken (Chancellor)
on Sep 28, 2016 at 05:54 UTC ( [id://1172801]=note: print w/replies, xml ) Need Help??


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

You read the file into memory, and you modify the in-memory copy. Now, what is required to make the changes appear in the file?

Hint: It's the same problem that every simple editor program has, and most editors are smart enough to ask you. Start the editor, open a text file, modify the copy in the editor's part of memory. Exit the editor. What does the editor ask you when attempting to exit?

Second hint: Read the synopsis of XML::DOM.

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Replies are listed 'Best First'.
Re^2: How to modify an XML file using XML::DOM
by ankit.tayal560 (Beadle) on Sep 28, 2016 at 06:23 UTC

    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);

      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'));

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1172801]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2024-03-19 09:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found