Original XML:
<library>
<email name="Peter P">Peter_Parker@library.com</email>
</library>
Expected Result:
<library>
<email name="Peter Parker">Peter.Parker@library.com</email>
<address>London</address>
</library>
Further XML challenge:
<library>
<email name="Peter Parker">Peter.Parker@library.com</email>
<address>
<housenumber>1</housenumber>
<street>Regent Street</street>
<postcode>AB12YZ</postcode>
</address>
</library>
Code:
use strict;
use XML::LibXML;
use XML::LibXML::NodeList;
my $parser = XML::LibXML->new;
my $doc = $parser->parse_file("StackTest.xml");
my $root = $doc->getDocumentElement();
#to modify email address
my $email = $doc->findnodes("/library/email");
my $text = XML::LibXML::Text->new('Peter.Parker@library.com');
$email->replaceNode($text);
#to modify email name attribute
my $email_attribute = $doc->findnodes("/library/email");
my $email_name_att->setAttribute(q|name|,"Peter Parker");
$email_attribute->getAttribute(q|name|);
#to add <address tag> with value
my $address = $doc->createElement('address');
$address->appendText('London');
$root->appendChild($address);
print($doc->toString);
Error message:
Can't locate object method "replaceNode" via package "XML::LibXML::NodeList"
I'm a beginner and new to Perl script. I'd like to modify the XML file using Perl and XML::LibXML module. I also visited CPAN but it's very difficult to grasp the concept with very little related examples. If you could provide me some hints that would be great to improve my knowledge.
Happy to get any sort of feedback and willing to learn :)
From the documentation of XML::LibXML::Node:
findnodes evaluates the xpath expression (XPath 1.0) on the current node and returns the resulting node set as an array. In scalar context, returns an XML::LibXML::NodeList object.
Your calls are in scalar context because of the $variable = $doc->findnodes(...)
.
You have three options:
my $el = $doc->find(...)
to return a single node.my ($el) = $doc->findnodes(...)
. This results in a call in list context and assigns the first element of the returned list to $el
.If there can be more nodes for your XPath expression, you can use a for
expression to loop over the result of ->findnodes(...)
like so:
for my $el ($doc->findnodes(...) {
print $el->tostring()
}