【问题标题】:Retrieve values from the reponse xml by GetGetElementByTheTagName通过 GetElementsByTagName 从响应 xml 中检索值
【发布时间】:2013-09-10 05:08:12
【问题描述】:
  Getting the  linkedin user profile

     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},

);

我得到了结果,但我想分别获取 id、名字、姓氏。如何使用 GetElementByTheTagName 来检索值。

【问题讨论】:

  • 你能在这里打印 $profile_xml 的值吗
  • bYdSs9C Jack Rudloph 123456789 2001 2 true vra Systems Information Technology and Services Information Technology and Services 0 像这样在一行中打印它作为 xml 内容接收,我们必须从中提取值。
  • 如果结果是数组还是xml格式?如果它在数组 $profile_xml[0]、$profile_xml[1]、$profile_xml[2] 中,将打印 id、first 和 lastnames。如果是 xml 那么你需要解析 xml 文件来获取值。
  • 不是数组格式。所以我必须在文件中写入然后必须提取或者是否有任何其他方法可以提取。
  • 如果是字符串,则可以拆分字符串:)

标签: xml perl


【解决方案1】:

如果您能够像这样管理 xml 内容:

 $str = '<id>bYdSs9C</id><first-name>Jack</first-name> <last-name>Rudloph</last-name>';

我们可以使用它来获取字段

while ($str =~ /<.*?>(.*?)<\/.*?>/g) {
        print $1, "\n";
}

【讨论】:

  • 你能解释一下它会做什么。为什么要问意味着它打印的东西和我展示的一样。
  • 嘿,首先确保字符串是上述格式,然后才起作用,while 循环在字符串中查找 info 格式并使用括号部分获取值: )。如果你想测试这个。使用上面的示例在您的实例上对其进行测试。如果可行,请投票:)
  • 我真的很困惑,您可以在 google 中通过类型 linkedin api cpan 查看原始源,然后转到链接并搜索您可以看到的 profile_xml。这样你就可以更清楚我问了什么。
【解决方案2】:

这里我展示了如何从响应中收到但不是由 GetElementbyTheTagname 接收的 xml 中解析链接的配置文件值。

  my $parser = XML::Parser->new( Style => 'Tree' );
  my $tree   = $parser->parse( $profile_xml );
  print Dumper( $tree );

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

这是我们可以从收到的响应中解析值的方式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-02
    • 1970-01-01
    • 2015-09-14
    • 1970-01-01
    • 1970-01-01
    • 2013-09-03
    相关资源
    最近更新 更多