Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

XML::LibXML::Node::addSibling() -- nNode is not a blessed SV reference ...

by mldvx4 (Friar)
on Sep 09, 2023 at 12:22 UTC ( [id://11154346] : perlquestion . print w/replies, xml ) Need Help??

mldvx4 has asked for the wisdom of the Perl Monks concerning the following question:

Greetings,

I'm branching out from working with HTML::TreeBuilder::XPath and HTML::Element which I am comfortable with. I am now trying to streamline some existing scripts by using XML::LibXML and XMLL::Element instead since that seems to allow transformation between various instances of XML more easily.

Or maybe not. There are some differences but by and large everything I have tried has gone well, and the parts I have refactored seem to run more quickly, except now I have become stumped by the error produced on line 19 in the script below, "XML::LibXML::Node::addSibling() -- nNode is not a blessed SV reference". How can I get past that and add a node?

#!/usr/bin/perl use XML::LibXML; use XML::Element; use strict; use warnings; # produces the following error: # XML::LibXML::Node::addSibling() -- nNode is not a blessed SV referen +ce ... my $html = XML::LibXML->load_xml( IO => *DATA{IO} ); foreach my $dd ($html->findnodes('//dd[*/*/a[@class="f"]]')) { if (my $f = $dd->find('./*/*/a[@class="f"]/@href')) { print qq(F=$f\n); my $cc = XML::Element->new('dd'); $cc->push_content('foo'); $dd->addSibling($cc); # this line fails } } print $html->toString,"\n"; exit(0); __DATA__ <html><body> <dl> <dd><details><summary>Example</summary> <dl> <dd><details><summary>one <a href="http://localhost/s1" class="s">0</a> <a href="http://localhost/f1" class="f">0</a> </summary></details></dd> <dd><details><summary>two <a href="http://localhost/s2" class="s">1</a> <a href="http://localhost/f2" class="f">1</a> </summary></details></dd> <dd><details><summary>three <a href="http://localhost/s3" class="s">2</a> <a href="http://localhost/f3" class="f">2</a> </summary></details></dd> </dl></details> </dd> </dl> </body></html>

The script successfully selects a specific set of dd elements below but cannot add a sibling to any of them, at least not the ways I've tried so far. I've tried quite a few permutations and variations on the theme, too.

Replies are listed 'Best First'.
Re: XML::LibXML::Node::addSibling() -- nNode is not a blessed SV reference ...
by tobyink (Canon) on Sep 09, 2023 at 13:09 UTC

      Thanks! That was the permutation I had missed. All set now.