-1
#!/usr/bin/perl -w

use WWW::LinkedIn;
use CGI;    # load CGI routines
use CGI::Session;
$q = CGI->new;                      # create new CGI object
print $q->header,                   # create the HTTP header
  $q->start_html('hello world'),    # start the HTML
  $q->h1('hello world'),            # level 1 header
  $q->end_html;                     # end the HTML
my $consumer_key    = 'xxxxxxx';
my $consumer_secret = 'xxxxxxxxx';
my $li              = WWW::LinkedIn->new(
    consumer_key    => $consumer_key,
    consumer_secret => $consumer_secret,
);

if ( length( $ENV{'QUERY_STRING'} ) > 0 ) { 
    $buffer = $ENV{'QUERY_STRING'};
    @pairs = split( /&/, $buffer );

    foreach $pair (@pairs) {
        ( $name, $value ) = split( /=/, $pair );
        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
        $in{$name} = $value;
    }   
    $sid = $q->cookie('CGISESSID') || $q->param('CGISESSID') || undef;

    $session = new CGI::Session( undef, $sid, { Directory => '/tmp' } );

    my $access_token = $li->get_access_token(
        verifier             => $in{'oauth_verifier'},
        request_token        => $session->param("request_token"),
        request_token_secret => $session->param("request_token_secret"),
    );  
    undef($session);
    my $profile_xml = $li->request(
        request_url =>
'http://api.linkedin.com/v1/people/~:(id,first-name,last-name,positions,industry,distance)',
        access_token        => $access_token->{token},
        access_token_secret => $access_token->{secret},
    );  
    print $profile_xml;
}

输出以单行打印。我想打印那是单独的行。

输出

aAVGFD34 jj DD 456456 2003 6 true ara systems Technology and Services  Technology and Services 0 

如何从 profile_xml 变量中获取每一列的值?

id avsdff
first name jj
lastname dd
4

3 回答 3

0

尝试从您的哈希参考中进行简单的打印

 foreach $key (keys %{$profile_xml}) {
     print "$key $profile_xml->{$key}\n";
 }
于 2013-09-07T18:51:12.330 回答
0

只需使用 Data::Dumper 和 XML::Simple。

use Data::Dumper; 
use XML::Simple; #you may want to install a specific package from your distribution

{...}
my $hash_ref = SimpeXML::XMLin($profile_xml);
print Dumper($hash_ref);

我不知道您是否想要更精美的输出。

于 2013-09-07T08:31:07.117 回答
0

在这里,我将展示解析数据并在各个行中打印的方式。

  my $parser = XML::Parser->new( Style => 'Tree' );
  my $tree   = $parser->parse( $profile_xml );
  #print Dumper( $tree ); you can use this see the data displayed in the tree  formatted

   my $UID = $tree->[1]->[4]->[2],"\n";
   print "User ID:$UID";
   print"</br>";

   my $FirstName = $tree->[1]->[8]->[2],"\n";
   print "First Name:$FirstName";
   print"</br>";

对于我展示的 UID 和名字的示例。这工作正常。

于 2013-09-11T10:07:55.783 回答