Home: Perl Programming Help: Intermediate:
Update an XML node value based on one of the attribute values.



amitp2011
New User

Apr 17, 2014, 10:39 PM


Views: 11598
Update an XML node value based on one of the attribute values.

I have to update the value of a node by based on the value of one of its attributes.

<ReferenceID referenceIDType="FXDD">FXDDILN233</ReferenceID>
For the above Tag.
i want to edit the value FXDDILN233 based on the attribute value referenceIDType="FXDD"

I tried the below code:

Code
my $nodeset = $doc->findnodes("//*[name()='ReferenceID']"); 
foreach my $node ($nodeset->get_nodelist) {
if ($node->hasAttribute("referenceIDType") == XML::LibXML::Boolean->True) {
$RetVal = $node->getAttributeNode("referenceIDType")->getValue();
if ($RetVal eq "FXDD") {
print "Attribute VAL=@@@@@@@@@@@@"."$RetVal \n";
$node->setData("$NodeVal");
}

}
}

Code
But getting error on the setData line.. 
Whats is it that could be wrong ?
Thanks in advance.


(This post was edited by amitp2011 on Apr 17, 2014, 11:18 PM)


amitp2011
New User

Apr 21, 2014, 4:36 AM


Views: 11016
Re: [amitp2011] Update an XML node value based on one of the attribute values.

I found the solution.

Code
my $nodeset = $doc->findnodes("//*[name()='ReferenceID']");  
foreach my $node ($nodeset->get_nodelist) {
if ($node->hasAttribute("referenceIDType") == XML::LibXML::Boolean->True) {
$RetVal = $node->getAttributeNode("referenceIDType")->getValue();
if ($RetVal eq "FXDD") {
print "Attribute VAL=@@@@@@@@@@@@"."$RetVal \n";
$node->firstChild->setData("blah blah"); [\b]
}

}
}



(This post was edited by amitp2011 on Apr 21, 2014, 4:37 AM)