in reply to Using XPath to Retrieve Remote XML Data

There's an advantage to XPath in that one can easily extract specific nodes that have specific attributes and get the specific component (i.e., a node in xml format or text within a tag) that one wants in a string.  Of course, you're probably right, if I knew more about what I'm doing I could work with just LibXML.  Nevertheless, with the help of the lovely and talented Miss jarich, I managed to resolve the problem, or should I say, I patched together LibXML and XPath to accomplish what I wanted.  Below is code added to the original node above that resolved the problem:

. . use XML::LibXML;# Plus the other modules above . . my $uri = "http://www.perlmonks.org/index.pl?node_id=30175&sinceunixti +me=20020820"; . # Parse out XML using LibXML to a string my $doc = $parser->parse_file($uri); my $root = $doc->getDocumentElement; my $file = $doc->toString; my $xpath = XML::XPath->new(xml=>"$file"); [...And resume the above code.]

There are three key changes here.  First, I had to change the URI address.  The uri I started with was a little messy:  I kept getting all kinds of extra HTML header stuff I couldn't skip over, causing it to crap out--I was probably doing it wrong, I admit.  Incidentally, the unixtime value should be calculated by code--I'll tend to that later.  The second key is to use the toString directive to save the matching XML tags and their text contents to $file.  The last and most significant key was to change the last line of the code shown in this post from the "filename" format directive (XML::XPath->new(filename=>$file))to the "xml" directive (XML::XPath->new(xml=>"$file")).  So, LibXML gets the XML from the PM site and then saves it to a string for XPath to read and extracts the desired nodes for standard Perl to work with.  I know this is the hard way to do it, but I'm still learning and would love for any one who so inclined to stream-line what I've done.  Otherwise, thanks for all the feedback and advice.

-Spenser

That's Spenser, with an "s" like the detective.

Replies are listed 'Best First'.
Re: Re: Retrieving Remote XML Data
by lestrrat (Deacon) on Aug 28, 2002 at 22:10 UTC

    um, I'm highly against that solution. You're loading XML::LibXML for fetching data only??? That's the wrong module to use. If you want to do that, use one of the LWP::* stuff

    On the other hand, I think you fail to realize the power that XML::LibXML gives to you. namely, It supports XPath notations!!! Did you read my earlier post? I do use some XPath notation to extract the "Discussion" nodes out of the XML.