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>

In reply to Error : can't locate object method "createElement" via package XML::DOM::ELEMENT by ankit.tayal560

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.