Simple xml file cr.xml
<book_reviewers>
<results>
<reviewer>
<name>Anne</name>
<profession>Catfish wrangler</profession>
</reviewer>
<reviewer>
<name>Bob</name>
<profession>Beer taster</profession>
</reviewer>
<reviewer>
<name>Charlie</name>
<profession>Gardener</profession>
</reviewer>
</results>
</book_reviewers>
I want to loop through each reviewer and replace the name with a new one
I am not trying to use both these methods, but going by other posts in the form both of these should work but I keep getting errors:
Can't locate object method "setData"
Can't locate object method "removeChildNodes
Can't locate object method "appendText"
#!/usr/bin/perl -w
use strict;
use XML::LibXML;
my $critics_file = "cr.xml";
my $parser = new XML::LibXML;
print "Couldn't retrieve review details\n"
unless my $book_reviews = $parser->parse_file($reviews_file);
foreach my $critics ($critic_details->findnodes('/book_reviewers/results/reviewer')) {
my $value = $critics->findvalue('name'); #returns the correct name
$value->removeChildNodes();
$value->appendText('new_name');
##ONLY EITHER THE ABOVE METHOD OR THE ONE BELOW - NOT BOTH
my $node = $critics->findnodes('.//name.text()');#returns the correct name
$node->setData('new_name');
}
Can anyone see where I am going wrong?
Cheers
The problem is that this call
returns the string content of the
name
element, and you are trying to treat it as anXML::LibXML::Element
object.Just change the method call to
and all should be well.
Here is a working version of your program.
output
Solution using
XML::LibXML
The following directly accesses the Text Node to simply and easily change the enclosed text:
Solution using
XML::Twig
Takes the name of each reviewer and just reverses them:
Outputs:
$value
is a simple scalar that holds the text of the found xml-element<name>…</name>
. You can not call any of those methods on a 'string', can you?Trying to get you into TIMTOWTDI, here is another example with more compact code:
You can find all the
<name>…</name>
elements directly, and you can iterate over them immediately, using the 'default' variable$_
.enjoy!