use strict;
use warnings FATAL => 'all';
use XML::LibXML;
use LWP::Simple;
# The XML structure...
# <myroot>
# <somenode>
# <somedeepernode>Hello, World!</somedeepernode>
# </somenode>
# </myroot>
my $rootNode = XML::LibXML::Element->new('myroot');
my $someNode = XML::LibXML::Element->new('somenode');
$rootNode->appendChild($someNode);
my $someDeeperNode = XML::LibXML::Element->new('somedeepernode');
$someDeeperNode->appendText('Hello, World!');
$someNode->appendChild($someDeeperNode);
my @someNodes = $rootNode->getElementsByTagName('somenode');
foreach my $someNode (@someNodes)
{
# If this line is commented, then all is good.
# But if it's left in... crash.
my $theText = $someNode->findvalue('somedeepernode');
# This line -- a different way to accomplish
# the same thing -- would crash too.
# my $theText = $someNode->getElementsByTagName('somedeepernode')-
>get_node(1)->nodeValue;
}
# Or if this line is commented, then all is good also. But because
I've had
# other weirdness with getting text from XML nodes, I'm leaning toward
LibXML
# being the culprit.
my $x = get('http://www.perl.com');
print q`If you're reading this, then it worked!`;
There are clearly issues/bugs with XML::LibXML.
I have run your program via valgrind memory profiler on a debian
system with perl 5.8 and there are numerous illegal memory usages
coming from the LibXML library.
Here is an example:
==2253== Invalid read of size 4
==2253== at 0x48338E6: domXPathFind (in /usr/lib/perl5/auto/XML/
LibXML/LibXML.so)
==2253== by 0x4814705: XS_XML__LibXML__Node__find (in /usr/lib/
perl5/auto/XML/LibXML/LibXML.so)
==2253== by 0x80BDAC0: Perl_pp_entersub (in /usr/bin/perl)
==2253== by 0x80BC398: Perl_runops_standard (in /usr/bin/perl)
==2253== by 0x8063BFC: perl_run (in /usr/bin/perl)
==2253== by 0x805FFD0: main (in /usr/bin/perl)
==2253== Address 0x476C534 is 20 bytes inside a block of size 88
free'd
==2253== at 0x401CFA5: free (vg_replace_malloc.c:233)
==2253== by 0x44093D6: xmlFreeDoc (in /usr/lib/libxml2.so.2.6.27)
==2253== by 0x483388D: domXPathFind (in /usr/lib/perl5/auto/XML/
LibXML/LibXML.so)
==2253== by 0x483393A: domXPathSelect (in /usr/lib/perl5/auto/XML/
LibXML/LibXML.so)
==2253== by 0x48141D6: XS_XML__LibXML__Node__findnodes (in /usr/lib/
perl5/auto/XML/LibXML/LibXML.so)
==2253== by 0x80BDAC0: Perl_pp_entersub (in /usr/bin/perl)
==2253== by 0x80BC398: Perl_runops_standard (in /usr/bin/perl)
==2253== by 0x8063BFC: perl_run (in /usr/bin/perl)
==2253== by 0x805FFD0: main (in /usr/bin/perl)
I would recommend using another module for XML handling and submitting
a bug to the maintainer of this package.
Regards,
Ivan Novick
http://www.0x4849.net
Wow. XML::LibXML is broken? Bummer.
XML::LibXML has a maintainer (me), so if you see something like this,
you better report it via rt.cpan.org than cry on usnet forums. Anyway,
I can confirm this problem exists with version 1.65 and earlier, but
in the SVN, it has been already fixed (due to somebody else's
report).
And by the way, the problem with the example code is that you do not
create a document node first. Make it
my $doc = XML::LibXML::Document->new;
$doc->createElement(...)
and things will start to work with 1.65 too.
Please wait for the next release or refer to
http://search.cpan.org/src/PAJAS/XML-LibXML-1.65/README
on how to install from SVN (the README assumes POSIX-like environment,
I fear it may be a bit trickier on windows).
-- Petr