I have a XML file, and I want to read it - remove a node - save it. I run perl from terminal (perl script.pl)
example XML (filename.xml):
<?xml version="1.0" encoding="UTF-8"?>
<twice>
<inner>
<twice>
<name>John</name>
<surname>Smith</surname>
</twice>
<twice>
<name>Alan</name>
<surname>Bowler</surname>
</twice>
<twice>
<name>Michael</name>
<surname>Deck</surname>
</twice>
</inner>
</twice>
example perl script (script.pl):
use strict;
use warnings;
use XML::LibXML;
my $filename = "filename.xml";
my $parser = XML::LibXML->new();
my $xmldoc = $parser->parse_file($filename);
for my $dead ($xmldoc->findnodes(q{/twice/inner/twice[surname = "Bowler"]})) {
$dead->unbindNode;
}
print $xmldoc->toString;
Now it outputs the expected result in terminal, but without saving the file.
Expected result (filename.xml):
<?xml version="1.0" encoding="UTF-8"?>
<twice>
<inner>
<twice>
<name>John</name>
<surname>Smith</surname>
</twice>
<twice>
<name>Michael</name>
<surname>Deck</surname>
</twice>
</inner>
</twice>
I have searched for many hours and couldn't find anything, sorry if it's a duplicate!
This is my first experience with perl so please any help would be welcomed, thanks.
When using toString the docs say to do it like this:
You can also use the
toFile()
function to save it:Edit: Also to quote the docs, (which is all I did) you can pass the format parameter to these functions.
Giving you:
or
You could also use App::Xml_grep2 to do this from the command line: