XML-LibXML - Re: getting setData to work

Posted on Wed Jun 18 23:53:49 2008 by guttersnipe in response to 7908 (See the whole thread of 4)
Re: getting setData to work
Cindy,

I was having this same issue when I was also trying to run the setData() method against the XML::LibXML::Element object. As you can see from the XML::LibXML::Element and XML::LibXML::Text documentation, the setData() method only exists in the latter.

There is, however, a wrapper method for the Element class called appendTextNode( $string ) that appends the argument $string to the end of the existing text for that element. Why there is no replaceTextNode() method escapes me. But, in the meantime, it seems that in order to replace instead of append, you need to be working with a Text object--not a Element object. This is accomplished by amending your xpath expression.

my @nodes=$doc->findnodes("//*[@lang='greek']");

After the above line executes, each element of the @nodes array will consist of an XML::LibXML::Element object. You want each of these elements to consist of an XML::LibXML::Text object. Change the line to:

my @nodes=$doc->findnodes("//*[@lang='greek']/text()");

...and wala! You can now iterate through the array of XML::LibXML::Text objects and--therefore--preform the setText() method on those objects.

Cheers!
Direct Responses: 8102 | Write a response