http://www.perlmonks.org?node_id=801184

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


Hi,


I am trying to use XML::LibXML for parsing an XML doc. I was not able to find a proper API documentation for XML::LibXML. Could any of you point me to a decent API doc please?

I was able to get the root node of an xml doc and find a node with a particular name. But I was unable to get the value of the node. What is the API to get the value (a string) of an XML node using XML::LibXML?


Replies are listed 'Best First'.
Re: Question on XML::LibXML...
by SilasTheMonk (Chaplain) on Oct 14, 2009 at 19:44 UTC
    I usually find the CPAN documentation adequate though you do have to read several modules: XML::LibXML::Element, XML::LibXML::Node etc. A separate issue is that your documentation problem may be with XPath and not XML::LibXML. I find http://www.w3schools.com/Xpath/ quite helpful in this regard. The real gotcha however is very often that when you try to parse a wild piece of XML, it often has a namespace set. The following example I have just grabbed out of some code of mine.
    my $parser = XML::LibXML->new(); my $doc = $parser->parse_string($xml_sitemap); print $doc->getDocumentElement->getNamespaces->getValue, "\n"; my $xpc = XML::LibXML::XPathContext->new(); $xpc->registerNs('x', $doc->getDocumentElement->getNamespaces->getValu +e); foreach my $u ($xpc->findnodes('//x:loc/child::text()',$doc)) { my $url = $u->toString; ....... }
Re: Question on XML::LibXML...
by ramrod (Curate) on Oct 14, 2009 at 19:58 UTC
    Sounds like you're looking for the text node. Use /text() in your xpath to reference this node.

    For example, if you want to point to a particular node:
    /root/element/text()
    Or more generically:
    //element/text()
    If you provide some sample xml, we could help out much more.