Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
laziness, impatience, and hubris
 
PerlMonks  

Having trouble groking XML::Parser::Lite::Tree::XPath

by jfroebe (Vicar)
on Feb 29, 2008 at 21:14 UTC ( #671294=perlquestion: print w/ replies, xml ) Need Help??
jfroebe has asked for the wisdom of the Perl Monks concerning the following question:

I'm extending the FlickrDownload script over in the code catacombs and am having trouble understanding how to use XML::Parser::Lite::Tree::XPath. Specifically, I'm trying to get the contents of the frob node.

#!/usr/bin/perl use strict; use warnings; use Config::Simple; use Flickr::API; use XML::Parser::Lite::Tree::XPath; my $cfg = new Config::Simple('FlickrAuthenticate.ini'); my $flickr_api_key = $cfg->param('Flickr.API_KEY'); my $flickr_secret = $cfg->param('Flickr.API_SHARED_SECRET'); my $flickr_email = $cfg->param('Flickr.email'); my $flickr_password = $cfg->param('Flickr.password'); my $api = new Flickr::API({'key' => $flickr_api_key, 'secret' => $flickr_secret}); my $response = $api->execute_method('flickr.auth.getFrob', { api_key => $flickr_api_key } ); my $xpath = new XML::Parser::Lite::Tree::XPath($response->{tree}); my @nodes = $xpath->select_nodes('/frob'); use Data::Dumper; print Dumper($xpath); print Dumper(\@nodes); print Dumper($nodes[0]->{children}); print Dumper($nodes[0]->{self});

output:

$VAR1 = bless( { 'max_order' => 6, 'tree' => { 'parent' => undef, 'order' => 1, 'name' => 'rsp', 'children' => [ { 'parent' => $VAR1->{'tr +ee'}, 'order' => 2, 'content' => ' ', 'children' => [], 'type' => 'data' }, { 'parent' => $VAR1->{'tr +ee'}, 'seq_num' => 1, 'name' => 'frob', 'children' => [ { 'pare +nt' => $VAR1->{'tree'}{'children'}[1], 'orde +r' => 4, 'cont +ent' => '72157604013085297-45646646467d-280706', 'chil +dren' => [], 'type +' => 'data' } ], 'seq_count' => 1, 'order' => 3, 'type' => 'tag', 'attributes' => {} }, { 'parent' => $VAR1->{'tr +ee'}, 'order' => 5, 'content' => ' ', 'children' => [], 'type' => 'data' } ], 'type' => 'tag', 'attributes' => { 'stat' => 'ok' } } }, 'XML::Parser::Lite::Tree::XPath' ); $VAR1 = [ { 'parent' => { 'parent' => undef, 'order' => 1, 'name' => 'rsp', 'children' => [ { 'parent' => $VAR1->[0]{'pa +rent'}, 'order' => 2, 'content' => ' ', 'children' => [], 'type' => 'data' }, $VAR1->[0], { 'parent' => $VAR1->[0]{'pa +rent'}, 'order' => 5, 'content' => ' ', 'children' => [], 'type' => 'data' } ], 'type' => 'tag', 'attributes' => { 'stat' => 'ok' } }, 'seq_num' => 1, 'name' => 'frob', 'children' => [ { 'parent' => $VAR1->[0], 'order' => 4, 'content' => '72157604013085297-45646646 +467d-280706', 'children' => [], 'type' => 'data' } ], 'seq_count' => 1, 'order' => 3, 'type' => 'tag', 'attributes' => {} } ]; $VAR1 = [ { 'parent' => { 'parent' => { 'parent' => undef, 'order' => 1, 'name' => 'rsp', 'children' => [ { 'parent' => +$VAR1->[0]{'parent'}{'parent'}, 'order' => 2 +, 'content' => + ' ', 'children' = +> [], 'type' => 'd +ata' }, $VAR1->[0]{'pa +rent'}, { 'parent' => +$VAR1->[0]{'parent'}{'parent'}, 'order' => 5 +, 'content' => + ' ', 'children' = +> [], 'type' => 'd +ata' } ], 'type' => 'tag', 'attributes' => { 'stat' => 'o +k' } }, 'seq_num' => 1, 'name' => 'frob', 'children' => $VAR1, 'seq_count' => 1, 'order' => 3, 'type' => 'tag', 'attributes' => {} }, 'order' => 4, 'content' => '72157604013085297-45646646467d-280706', 'children' => [], 'type' => 'data' } ]; $VAR1 = undef;
How do I get at the contents?

Solved!

#!/usr/bin/perl use strict; use warnings; use Config::Simple; use Flickr::API; use XML::Parser::Lite::Tree::XPath; my $cfg = new Config::Simple('FlickrAuthenticate.ini'); my $flickr_api_key = $cfg->param('Flickr.API_KEY'); my $flickr_secret = $cfg->param('Flickr.API_SHARED_SECRET'); my $flickr_email = $cfg->param('Flickr.email'); my $flickr_password = $cfg->param('Flickr.password'); my $api = new Flickr::API({'key' => $flickr_api_key, 'secret' => $flickr_secret}); my $response = $api->execute_method('flickr.auth.getFrob', { api_key => $flickr_api_key } ); my $xpath = new XML::Parser::Lite::Tree::XPath($response->{tree}); my @nodes = $xpath->select_nodes('/frob'); my $frob = $nodes[0]->{children}[0]->{content}; print "Frob: $frob\n";

Jason L. Froebe

Blog, Tech Blog

Comment on Having trouble groking XML::Parser::Lite::Tree::XPath
Select or Download Code
Re: Having trouble groking XML::Parser::Lite::Tree::XPath
by ikegami (Pope) on Feb 29, 2008 at 22:29 UTC

    It's hard to tell since you didn't provide the XML, but the path would normally be /frob/text() or even /frob[1]/text() to short-circuit. However, that module doesn't seem to provide text() (Lite indeed!), nor does it seem to return object (which might have a text method), so $nodes[0]{children}[0]{content} is indeed the way to go.

    By the way, the arrows are optional between indexes. For example, the following three expressions are identical.

    $nodes[0]{children}[0]{content} Implicit $nodes[0]->{children}[0]->{content} Somewhere in between $nodes[0]->{children}->[0]->{content} Explicit

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (11)
As of 2014-04-17 07:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (440 votes), past polls