Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Question on XML::LibXML...

by biswanath_c (Beadle)
on Oct 14, 2009 at 19:33 UTC ( #801184=perlquestion: print w/ replies, xml ) Need Help??
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?


Comment on Question on XML::LibXML...
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 (Hermit) 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://801184]
Approved by SilasTheMonk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (13)
As of 2014-08-27 19:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (250 votes), past polls