in reply to Update an XML node value based on one of the attribute values.

XML::LibXML::Element doesn't have a setData method. XML::LibXML::Text, XML::LibXML::Comment, and XML::LibXML::PI do. I think perhaps you want:

$node->firstChild->setData("blahblah");

Here's a demo:

use strict; use warnings; use XML::LibXML 2; my $doc = 'XML::LibXML'->load_xml(IO => \*DATA); foreach my $node ( $doc->findnodes('//ReferenceID') ) { $node->firstChild->setData("Reference ID type is $node->{reference +IDType}") if $node->{referenceIDType} eq 'FXDD'; } print $doc->toString; __DATA__ <root> <ReferenceID referenceIDType="FXDD">FXDDILN233</ReferenceID> </root>
use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

Replies are listed 'Best First'.
Re^2: Update an XML node value based on one of the attribute values.
by amitp2011 (Initiate) on Apr 18, 2014 at 09:49 UTC
    But where are we setting the value of node ReferenceID.

      amitp2011:

      He's not setting it--tobyink was simply showing you how to get the desired element that has the setData method. Frequently, when answering questions, we show you how to get past the stumbling point, we figure you can take it from there. ;^)

      By the way, if you would, please edit your first post in this thread and remove the blank from the closing </Code> tag.change the code tags from <Code> to <code>, so the code displays correctly. (Don't forget to change the closing tag, too!)

      Update: ambrus pointed out that the problem in the original post was that the problem wasn't the case, but that there was an extra blank in the closing tag.

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

        amitp2011 also appears to be using the  &lt; entity rather than a literal  '<' to begin the  <code> tag — or is the presence of this entity in the viewed source a rendering artifact?

        For the code from tobyink
        use strict; use warnings; use XML::LibXML 2; my $doc = 'XML::LibXML'->load_xml(IO => \*DATA); foreach my $node ( $doc->findnodes('//ReferenceID') ) { $node->firstChild->setData("Reference ID type is $node->{reference +IDType}") if $node->{referenceIDType} eq 'FXDD'; } print $doc->toString;

        when i use $node ->setData("xyz"); i again get the same error. Please excuse my ignorance, i am very new to perl scripting.
Re^2: Update an XML node value based on one of the attribute values.
by Anonymous Monk on Apr 21, 2014 at 07:23 UTC
    Thanks.. my issue resolved.

    $node->firstChild->setData("blahblah");

    did the trick :)