Subject: | XML::DOM::Node::toString does not work at the document root level |
The following simple piece of code does not work:
-- CODE BEGINS --
use XML::DOM::XPath;
$xml = XML::DOM::Parser->new->parse('<root>hello world</root>');
print $xml->toString;
-- CODE ENDS --
This will produce the following error message:
Can't locate object method "print" via package "XML::XPath" (perhaps you forgot to load "XML::XPath"?) at /usr/local/perl561/lib/site_perl/5.6.1/XML/DOM.pm line 3624.
But the same code using XML::DOM works flawlessly:
-- CODE BEGINS --
use XML::DOM;
$xml = XML::DOM::Parser->new->parse('<root>hello world</root>');
print $xml->toString;
-- CODE ENDS --
This will print the string "<root>hello world</root>" as intended.
The workaround for me right now is to loop through the child nodes and print them one by one instead:
-- CODE BEGINS --
use XML::DOM::XPath;
$xml = XML::DOM::Parser->new->parse('<root>hello world</root>');
print $_->toString for $xml->getChildNodes;
-- CODE ENDS --
And no error will occur.
I love this hybrid DOM/XPath module but this bug can really be annoying at times. Unfortuntely I don't know enough about the XML::DOM and XML::XPath's internals to debug it myself. Hopefully somebody can help.
By the way I'm running Perl 5.6.1 built for i686-linux, with XML-DOM-1.43, XML-XPath-1.13 and XML-DOM-XPath-0.05 installed.
Thanks a lot!