Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Error : can't locate object method "createElement" via package XML::DOM::ELEMENT

by ankit.tayal560 (Beadle)
on Sep 29, 2016 at 04:33 UTC ( [id://1172885]=perlquestion: print w/replies, xml ) Need Help??

ankit.tayal560 has asked for the wisdom of the Perl Monks concerning the following question:

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>
  • Comment on Error : can't locate object method "createElement" via package XML::DOM::ELEMENT
  • Download Code

Replies are listed 'Best First'.
Re: Error : can't locate object method "createElement" via package XML::DOM::ELEMENT
by NetWallah (Canon) on Sep 29, 2016 at 05:54 UTC
    Try using $doc, instead of $root.

    createElement is a method of XML::DOM::Document, not of 'XML::DOM::Element', which is what $root is.

            ...it is unhealthy to remain near things that are in the process of blowing up.     man page for WARP, by Larry Wall

      ohh eya right! thanks a lot . worked (y)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-26 00:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found