I’m trying to add a new node to an existing XML document.
However, trying to use the push function in a XML::LibXML::NodeList result does not change the document.
Here’s an example:
#!/usr/bin/perl
use strict;
use XML::LibXML;
my $parser = XML::LibXML->new();
my $xml_string =
'<example>
<books>
<category id="1">
<book isbn="a" />
<book isbn="b" />
<book isbn="c" />
</category>
<category id="2"/>
<category id="3"/>
</books>
</example>';
my $doc = $parser->parse_string($xml_string);
my $category_nodelist = $doc->findnodes('//category[@id="1"]');
my $book_el = $doc->ownerDocument->createElement('book');
$book_el->setAttribute("isbn", "d");
$category_nodelist->push($book_el);
print $doc->toString(1);
To insert the new node into the document, use