I have an xml file as shown below:
<root>
<element1>abc</element1>
<element2>123</element2>
<element3>456</element3>
</root>
I am trying to add and element4 in perl using xml:dom
use XML::DOM;
#parse the file
my $parser = new XML::DOM::Parser;
my $doc = $parser->parsefile ("mytest.xml");
my $root = $doc->getDocumentElement();
my $new_element= $doc->createElement("element4");
my $new_element_text= $doc->createTextNode('testing');
$new_element->appendChild($new_element_text);
$root->appendChild($new_element);
I am getting the error: "Undefined subroutine &XML::LibXML::Element::getNodeType "
i tried insetBefore method to, by finding elements and tried to insert it before that.
Any pointers, what am i doing wrong?
It worked fine for me, and didn't read XML::LibXML (it used XML::Parser::Expat). I have XML::DOM version 1.44 installed.
Of course, you could try installing XML::LibXML and see if that fixes the problem.
I think the simplest way to do this is with XML::Simple
XML::DOM seems to be last updated in 2000, which means it is not very much supported module. It looks like XML::LibXML provides very similar interface, see below working example: