I am using libXML to update a batch of XML settings files. Here is an example of the section of xml I am trying to update:
<Setting SID="2091">
<Name>HME Cloud User Email</Name>
<Value/>
</Setting>
I can properly update that section with the following code.
#!/usr/bin/perl
use File::Basename;
use XML::LibXML;
use XML::LibXML::XPathContext;
@files = glob('C:\Users\1025144\Documents\timer_test\*.xml');
foreach $file (@files)
{
my $dom = XML::LibXML->load_xml(location => $file);
my $xpc = XML::LibXML::XPathContext->new($dom);
$xpc->registerNs('xsd', 'http://hme.com/Settings.xsd');
#Add email
my($match9) = $xpc->findnodes('//xsd:Settings/xsd:Setting[@SID="2091"]/xsd:Value');
$match9->removeChildNodes();
$match9->appendText('[email protected]');
open (OUTFILE, ">$file");
print OUTFILE $dom;
}
print "XML Files Updated."
but the issue is some of the settings files are missing the Value node for this SID, see example below:
<Setting SID="2091">
<Name>HME Cloud User Email</Name>
</Setting>
I have been looking at the addChild to attempt to place the node into this SID but I cant seem to figure out the syntax. I use perl off and on so I am hoping someone can help me figure out how to add this node or if there is an alternative solution that I can use if the SID has the Value node or not.
Any help is appreciated - Thanks!
Use an
if
, if the Value doesn't exist, create it usingaddNewChild
.Notes:
toFile
for printing XML.