Here is a basic XML document example
<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>
And I want to add this:
<reviewer>
<name>Joan</name>
<profession>Jett</profession>
</reviewer>
I have tried a combinations of many solutions, here is one that, at least, doesn't throw an error, but, it also doesn't work.
#!/usr/bin/perl
use XML::LibXML;
use strict;
my $filename = "cr.xml";
my $parser = XML::LibXML->new();
my $critic_details = $parser->parse_file("$filename") or die;
my $new_reviewer = $critic_details->documentElement;
my $reviewer_name = $critic_details->documentElement;
my $reviewer_prof = $critic_details->documentElement;
my $newnode = $critic_details->documentElement;
for my $reviewers($critic_details->findnodes("book_reviewers/results/reviewers")){
$new_reviewer = $reviewers->createElement("reviewer");
$reviewer_name = $new_reviewer->addChild("name");
$reviewer_name->appendText("Joan");
$reviewer_prof = $new_reviewer->addChild("profession");
$reviewer_prof->appendText("Jett");
$newnode = $reviewers->addSibling($new_reviewer); #also tried addChild
}
print $critic_details->toString;
The output that i get is:
<?xml version="1.0"?>
<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>
Which is just the original data
Any help greatly appreciated - I am very new to both Perl and XML Cheers