in reply to Re: Missing Node.PM in XML::LibXML v1.69
in thread Missing Node.PM in XML::LibXML v1.69

I do. I get

Can't locate object method "getFirstChild" via package "XML::LibXML::NodeList"

from my test script.

  • Comment on Re^2: Missing Node.PM in XML::LibXML v1.69

Replies are listed 'Best First'.
Re^3: Missing Node.PM in XML::LibXML v1.69
by ikegami (Patriarch) on Jun 12, 2010 at 00:02 UTC

    XML::LibXML::NodeList isn't XML::LibXML::Node.

    You are probably using findnodes in scalar context.

    my @nodes = $doc->findnodes(...); # Return all matching nodes my $nodes = $doc->findnodes(...); # Ditto, as a NodeList

    If you want the first match node, call findnodes in list context.

    my ($node) = $doc->findnodes(...); # Get first matching node.

    If you want to visit every matching node, the following is probably more what you want:

    for my $node ($doc->findnodes(...)) { ... }

      That was it... I had my context wrong for the findnodes function:

      Old:

      my $status = $dom->findnodes("..."); my $msg = $dom->findnodes("..."); . . print $status->getFirstChild()->data, "\n"; print $msg->getFirstChild()->data, "\n";

      New:

      my ($status) = $dom->findnodes("..."); my ($msg) = $dom->findnodes("..."); . . print $status->getFirstChild()->nodeValue(), "\n"; print $msg->getFirstChild()->nodeValue(), "\n";

      Thanks, everyone, for all the help.

        I'm guessing

        ->getFirstChild()->nodeValue()

        could be replaced with

        ->textContent()
Re^3: Missing Node.PM in XML::LibXML v1.69
by almut (Canon) on Jun 11, 2010 at 21:55 UTC

    You're presumably calling it on the wrong object type.  What does your code/xml look like?

    Here's a simple example using getFirstChild that works:

    #!/usr/bin/perl use strict; use warnings; use XML::LibXML; my $parser = XML::LibXML->new(); my $doc = $parser->parse_fh(\*DATA); for my $elem ( $doc->getElementsByTagName('foo') ) { my $val = $elem->getFirstChild()->nodeValue(); print "$val\n"; # Foo1 Foo2 } __DATA__ <?xml version = "1.0" encoding="UTF-8" ?> <doc> <foo>Foo1<bar>Bar</bar></foo> <foo>Foo2</foo> </doc>
Re^3: Missing Node.PM in XML::LibXML v1.69
by Anonymous Monk on Jun 11, 2010 at 23:28 UTC