XML-LibXML - getting setData to work

Posted on Tue May 20 01:15:12 2008 by digitalramble
getting setData to work
OK, I'm having a bit of a problem getting setData to work. I can *clearly* use it -- it works fine if I copy the example given at http://www.perlmonks.org/?node_id=490846
In particular the example near the bottom of that page where it changes the page value to 394. That all works, so I'm sure my setup etc, is okay. However, when I try to tweak this for my purposes, I run into a stone wall, and it just keeps telling me
Can't locate object method "setData" via package "XML::LibXML::Element"
(and yes, I googled that up...zilch).

Now, in the example, the xpath expression locates one single node. In my application, I'm interested in finding all nodes with a particular attribute and changing those texts around a little bit. I have established that the xpath expression does indeed pull up all the correct nodes from the xml file (via printing out the contents) and I know that the change function works. But adding in the last setData to change the text goes belly up.

Here's my code snippet.

eval { $doc = $parser->parse_file($file); }; warn $@ if $@; my $nodes=$doc->findnodes('//*[@lang="greek"]'); foreach my $node ($nodes->get_nodelist) { my $content=$node->textContent; print "$content\n"; my $changedtext = changeold2new($content); print "$changedtext\n"; $node->setData($betacode); }

It just seems like the node "type" for setData is different than the node "type" for textContent but at this point, I just seem to be going in circles looking through the documentation at cpan to figure out what goes with what or how to convert or...WHAT. Help! I'm sure it's something stupidly obvious, just because :-P.

Thanks, --Cindy
Direct Responses: 8100 | Write a response