i have tried to use the LibXML modules to xpath a document but its getting complicated ..........
i have seen this example over forum here .........
use XML::LibXML ;
use strict;
use warnings;
{
my $xml = <<'XML';
<?xml version="1.0" standalone="yes"?>
<sdnList>
<sdnEntry>
<lastName>Hello world!</lastName>
</sdnEntry>
</sdnList>
XML
my $parser = XML::LibXML->new;
my $doc = $parser->parse_string($xml);
my $result = $doc->findvalue('//lastName');
print $result;
}
{
my $xml = <<'XML';
<?xml version="1.0" standalone="yes"?>
<sdnList xmlns="http://tempuri.org/sdnList.xsd">
<sdnEntry>
<lastName>Hello world!</lastName>
</sdnEntry>
</sdnList>
XML
my $parser = XML::LibXML->new;
my $doc = $parser->parse_string($xml);
my $result = $doc->findvalue('//lastName');
print $result ;
}
/
One of the member posted this solution ... /
use XML::LibXML;
use XML::LibXML::XPathContext;
{
my $xml = <<'XML';
<?xml version="1.0" standalone="yes"?>
<sdnList xmlns="http://tempuri.org/sdnList.xsd">
<sdnEntry>
<lastName>Hello world!</lastName>
</sdnEntry>
</sdnList>
XML
my $parser = XML::LibXML->new;
my $doc = $parser->parse_string($xml);
my $xc = XML::LibXML::XPathContext->new($doc);
$xc->registerNs('sdnList', 'http://tempuri.org/sdnList.xsd');
my $result = $xc->findvalue('//sdnList:lastName');
is( $result, "Hello world!", "Namespace" );
}
But every time we can't get same namespace ... if i add any API to the xml input ..... its extremely difficult to handle it ......
Is there any way to get nodes and attr and like that stuff in XML::LibXML easily ??? or any other modules excpet xml::xpath..
UPDATE ...... i think i have not conveyed the message properly ... about my last part .. .
if the XML page varies every time ... like this ....
lets take one example ... Flickr API http://www.flickr.com/services/api/explore/?method=flickr.photos.getSizes .... here we go .... if give the correct Photo ID i will get this result ...
<rsp stat="ok">
<sizes canblog="1" canprint="0" candownload="0">
<size label="Square" width="75" height="75" source="http://farm5.stati
+c.flickr.com/4007/4673769513_02826f0775_s.jpg" url="http://www.flickr
+.com/photos/dhushor-jen/4673769513/sizes/sq/" media="photo"/>
<size label="Thumbnail" width="100" height="67" source="http://farm5.s
+tatic.flickr.com/4007/4673769513_02826f0775_t.jpg" url="http://www.fl
+ickr.com/photos/dhushor-jen/4673769513/sizes/t/" media="photo"/>
<size label="Small" width="240" height="160" source="http://farm5.stat
+ic.flickr.com/4007/4673769513_02826f0775_m.jpg" url="http://www.flick
+r.com/photos/dhushor-jen/4673769513/sizes/s/" media="photo"/>
<size label="Medium" width="500" height="333" source="http://farm5.sta
+tic.flickr.com/4007/4673769513_02826f0775.jpg" url="http://www.flickr
+.com/photos/dhushor-jen/4673769513/sizes/m/" media="photo"/>
<size label="Large" width="1024" height="683" source="http://farm5.sta
+tic.flickr.com/4007/4673769513_02826f0775_b.jpg" url="http://www.flic
+kr.com/photos/dhushor-jen/4673769513/sizes/l/" media="photo"/>
</sizes>
</rsp>
what if i give incorrect photo id in the above code ....
<rsp stat="fail">
<err code="1" msg="Photo not found"/>
</rsp>
Can any one tell me how can write xpath this kind of things ?
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.