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

I do not get the Node.pm module, which contains methods like getFirstChild()

Do you get an error when you try to use getFirstChild()?  The method is in LibXML.xs:

SV* firstChild( self ) xmlNodePtr self ALIAS: getFirstChild = 1 CODE: PERL_UNUSED_VAR(ix); RETVAL = PmmNodeToSv( self->children, PmmOWNERPO( PmmPROXYNODE(self) ) ); OUTPUT: RETVAL

so things should be fine (there mustn't necessarily be a Node.pm file for the method to work).

Replies are listed 'Best First'.
Re^2: Missing Node.PM in XML::LibXML v1.69
by BluePerlDev (Novice) on Jun 11, 2010 at 20:54 UTC

    I do. I get

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

    from my test script.

      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.

      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>