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


In reply to Having trouble groking XML::Parser::Lite::Tree::XPath by jfroebe

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.