如何解析具有xml数据轨道的变量?

时间:2013-05-07 06:38:57

标签: ruby-on-rails xml linkedin controllers

我必须从具有以下

的变量'user'中检索值
 #<LinkedIn::Profile:0x00000001758bd0 @doc=#<Nokogiri::XML::Document:0xbac674 name="document" children=[#<Nokogiri::XML::Element:0xbac534 name="person" children=[#<Nokogiri::XML::Text:0xbb0c38 "\n  ">, #<Nokogiri::XML::Element:0xbac110 name="positions" attributes=[#<Nokogiri::XML::Attr:0xbb0a6c name="total" value="2">] children=[#<Nokogiri::XML::Text:0xbabc9c "\n    ">, #<Nokogiri::XML::Element:0xbabc88 name="position" children=[#<Nokogiri::XML::Text:0xbafc34 "\n      ">, #<Nokogiri::XML::Element:0xbb40a4 name="id" children=[#<Nokogiri::XML::Text:0xbb3c94 "337249901">]>, #<Nokogiri::XML::Text:0xbb3a00 "\n      ">, #<Nokogiri::XML::Element:0xbb38e8 name="title" children=[#<Nokogiri::XML::Text:0xbb3668 "Software Developer Trainee">]>, #<Nokogiri::XML::Text:0xbb33c0 "\n      ">, #<Nokogiri::XML::Element:0xbb321c name="summary" children=[#<Nokogiri::XML::Text:0xbb62a0 "I am a developing ruby on rails application and Infosec products.">]>, #<Nokogiri::XML::Text:0xbb5954 "\n      ">, #<Nokogiri::XML::Element:0xbb5760 name="start-date" children=[#<Nokogiri::XML::Text:0xbb52c4 "\n        ">, #<Nokogiri::XML::Element:0xbb5044 name="year" children=[#<Nokogiri::XML::Text:0xbb4b94 "2012">]>, #<Nokogiri::XML::Text:0xbb47e8 "\n        ">, #<Nokogiri::XML::Element:0xbb46bc name="month" children=[#<Nokogiri::XML::Text:0xbb43d8 "8">]>, #<Nokogiri::XML::Text:0xbbb750 "\n      ">]>, #<Nokogiri::XML::Text:0xbbb188 "\n      ">, #<Nokogiri::XML::Element:0xbbaf6c name="is-current" children=[#<Nokogiri::XML::Text:0xbba288 "true">]>, #<Nokogiri::XML::Text:0xbb9c70 "\n      ">, #<Nokogiri::XML::Element:0xbbe0a4 name="company" children=[#<Nokogiri::XML::Text:0xbbd6e0 "\n        ">, #<Nokogiri::XML::Element:0xbbd348 name="id" children=[#<Nokogiri::XML::Text:0xbbcda8 "260344">]>, #<Nokogiri::XML::Text:0xbbc6f0 "\n        ">, #<Nokogiri::XML::Element:0xbc2c6c name="name" children=[#<Nokogiri::XML::Text:0xbc28e8 "Torrid Networks Private Limited">]>, #<Nokogiri::XML::Text:0xbc2348 "\n        ">, #<Nokogiri::XML::Element:0xbc1e34 name="size" children=[#<Nokogiri::XML::Text:0xbc12b8 "11-50 employees">]>, #<Nokogiri::XML::Text:0xbc0e94 "\n        ">, #<Nokogiri::XML::Element:0xbc53f4 name="type" children=[#<Nokogiri::XML::Text:0xbc505c "Privately Held">]>, #<Nokogiri::XML::Text:0xbc4bac "\n        ">, #<Nokogiri::XML::Element:0xbc49f4 name="industry" children=[#<Nokogiri::XML::Text:0xbc44e0 "Computer & Network Security">]>, #<Nokogiri::XML::Text:0xbc4008 "\n      ">]>, #<Nokogiri::XML::Text:0xbc398c "\n    ">]>, #<Nokogiri::XML::Text:0xbabb84 "\n    ">, #<Nokogiri::XML::Element:0xbabb48 name="position" children=[#<Nokogiri::XML::Text:0xbcca8c "\n      ">, #<Nokogiri::XML::Element:0xbcc8e8 name="id" children=[#<Nokogiri::XML::Text:0xbcc44c "340068235">]>, #<Nokogiri::XML::Text:0xbcbd80 "\n      ">, #<Nokogiri::XML::Element:0xbcbab0 name="title" children=[#<Nokogiri::XML::Text:0xbd1604 "Software Developer Trainee">]>, #<Nokogiri::XML::Text:0xbd0eac "\n      ">, #<Nokogiri::XML::Element:0xbd0c7c name="summary" children=[#<Nokogiri::XML::Text:0xbd0704 "I am a Ruby On Rails Developer">]>, #<Nokogiri::XML::Text:0xbd4020 "\n      ">, #<Nokogiri::XML::Element:0xbd3cd8 name="start-date" children=[#<Nokogiri::XML::Text:0xbd3724 "\n        ">, #<Nokogiri::XML::Element:0xbd3364 name="year" children=[#<Nokogiri::XML::Text:0xbd3008 "2012">]>, #<Nokogiri::XML::Text:0xbd27c0 "\n        ">, #<Nokogiri::XML::Element:0xbd2518 name="month" children=[#<Nokogiri::XML::Text:0xbd89cc "8">]>, #<Nokogiri::XML::Text:0xbd8530 "\n      ">]>, #<Nokogiri::XML::Text:0xbd7fa4 "\n      ">, #<Nokogiri::XML::Element:0xbd7e28 name="is-current" children=[#<Nokogiri::XML::Text:0xbd7a90 "true">]>, #<Nokogiri::XML::Text:0xbd7554 "\n      ">, #<Nokogiri::XML::Element:0xbd7324 name="company" children=[#<Nokogiri::XML::Text:0xbd6fb4 "\n        ">, #<Nokogiri::XML::Element:0xbd6dd4 name="id" children=[#<Nokogiri::XML::Text:0xbda9fc "260344">]>, #<Nokogiri::XML::Text:0xbda63c "\n        ">, #<Nokogiri::XML::Element:0xbda470 name="name" children=[#<Nokogiri::XML::Text:0xbd9c50 "Torrid Networks Private Limited">]>, #<Nokogiri::XML::Text:0xbd94d0 "\n        ">, #<Nokogiri::XML::Element:0xbd92f0 name="size" children=[#<Nokogiri::XML::Text:0xbdd47c "11-50 employees">]>, #<Nokogiri::XML::Text:0xbdcf40 "\n        ">, #<Nokogiri::XML::Element:0xbdcd74 name="type" children=[#<Nokogiri::XML::Text:0xbdc978 "Privately Held">]>, #<Nokogiri::XML::Text:0xbdbf28 "\n        ">, #<Nokogiri::XML::Element:0xbdbc44 name="industry" children=[#<Nokogiri::XML::Text:0xbe2710 "Computer & Network Security">]>, #<Nokogiri::XML::Text:0xbe2364 "\n      ">]>, #<Nokogiri::XML::Text:0xbe1dec "\n    ">]>, #<Nokogiri::XML::Text:0xbabb0c "\n  ">]>, #<Nokogiri::XML::Text:0xbe1054 "\n">]>]>, @positions=[#<LinkedIn::Position::Resource:0x00000001757870 @position=#<Nokogiri::XML::Element:0xbabc88 name="position" children=[#<Nokogiri::XML::Text:0xbafc34 "\n      ">, #<Nokogiri::XML::Element:0xbb40a4 name="id" children=[#<Nokogiri::XML::Text:0xbb3c94 "337249901">]>, #<Nokogiri::XML::Text:0xbb3a00 "\n      ">, #<Nokogiri::XML::Element:0xbb38e8 name="title" children=[#<Nokogiri::XML::Text:0xbb3668 "Software Developer Trainee">]>, #<Nokogiri::XML::Text:0xbb33c0 "\n      ">, #<Nokogiri::XML::Element:0xbb321c name="summary" children=[#<Nokogiri::XML::Text:0xbb62a0 "I am a developing ruby on rails application and Infosec products.">]>, #<Nokogiri::XML::Text:0xbb5954 "\n      ">, #<Nokogiri::XML::Element:0xbb5760 name="start-date" children=[#<Nokogiri::XML::Text:0xbb52c4 "\n        ">, #<Nokogiri::XML::Element:0xbb5044 name="year" children=[#<Nokogiri::XML::Text:0xbb4b94 "2012">]>, #<Nokogiri::XML::Text:0xbb47e8 "\n        ">, #<Nokogiri::XML::Element:0xbb46bc name="month" children=[#<Nokogiri::XML::Text:0xbb43d8 "8">]>, #<Nokogiri::XML::Text:0xbbb750 "\n      ">]>, #<Nokogiri::XML::Text:0xbbb188 "\n      ">, #<Nokogiri::XML::Element:0xbbaf6c name="is-current" children=[#<Nokogiri::XML::Text:0xbba288 "true">]>, #<Nokogiri::XML::Text:0xbb9c70 "\n      ">, #<Nokogiri::XML::Element:0xbbe0a4 name="company" children=[#<Nokogiri::XML::Text:0xbbd6e0 "\n        ">, #<Nokogiri::XML::Element:0xbbd348 name="id" children=[#<Nokogiri::XML::Text:0xbbcda8 "260344">]>, #<Nokogiri::XML::Text:0xbbc6f0 "\n        ">, #<Nokogiri::XML::Element:0xbc2c6c name="name" children=[#<Nokogiri::XML::Text:0xbc28e8 "Torrid Networks Private Limited">]>, #<Nokogiri::XML::Text:0xbc2348 "\n        ">, #<Nokogiri::XML::Element:0xbc1e34 name="size" children=[#<Nokogiri::XML::Text:0xbc12b8 "11-50 employees">]>, #<Nokogiri::XML::Text:0xbc0e94 "\n        ">, #<Nokogiri::XML::Element:0xbc53f4 name="type" children=[#<Nokogiri::XML::Text:0xbc505c "Privately Held">]>, #<Nokogiri::XML::Text:0xbc4bac "\n        ">, #<Nokogiri::XML::Element:0xbc49f4 name="industry" children=[#<Nokogiri::XML::Text:0xbc44e0 "Computer & Network Security">]>, #<Nokogiri::XML::Text:0xbc4008 "\n      ">]>, #<Nokogiri::XML::Text:0xbc398c "\n    ">]>, @company=#<LinkedIn::Company:0x000000017564e8 @doc=[#<Nokogiri::XML::Element:0xbbe0a4 name="company" children=[#<Nokogiri::XML::Text:0xbbd6e0 "\n        ">, #<Nokogiri::XML::Element:0xbbd348 name="id" children=[#<Nokogiri::XML::Text:0xbbcda8 "260344">]>, #<Nokogiri::XML::Text:0xbbc6f0 "\n        ">, #<Nokogiri::XML::Element:0xbc2c6c name="name" children=[#<Nokogiri::XML::Text:0xbc28e8 "Torrid Networks Private Limited">]>, #<Nokogiri::XML::Text:0xbc2348 "\n        ">, #<Nokogiri::XML::Element:0xbc1e34 name="size" children=[#<Nokogiri::XML::Text:0xbc12b8 "11-50 employees">]>, #<Nokogiri::XML::Text:0xbc0e94 "\n        ">, #<Nokogiri::XML::Element:0xbc53f4 name="type" children=[#<Nokogiri::XML::Text:0xbc505c "Privately Held">]>, #<Nokogiri::XML::Text:0xbc4bac "\n        ">, #<Nokogiri::XML::Element:0xbc49f4 name="industry" children=[#<Nokogiri::XML::Text:0xbc44e0 "Computer & Network Security">]>, #<Nokogiri::XML::Text:0xbc4008 "\n      ">]>]>>, #<LinkedIn::Position::Resource:0x00000001757668 @position=#<Nokogiri::XML::Element:0xbabb48 name="position" children=[#<Nokogiri::XML::Text:0xbcca8c "\n      ">, #<Nokogiri::XML::Element:0xbcc8e8 name="id" children=[#<Nokogiri::XML::Text:0xbcc44c "340068235">]>, #<Nokogiri::XML::Text:0xbcbd80 "\n      ">, #<Nokogiri::XML::Element:0xbcbab0 name="title" children=[#<Nokogiri::XML::Text:0xbd1604 "Software Developer Trainee">]>, #<Nokogiri::XML::Text:0xbd0eac "\n      ">, #<Nokogiri::XML::Element:0xbd0c7c name="summary" children=[#<Nokogiri::XML::Text:0xbd0704 "I am a Ruby On Rails Developer">]>, #<Nokogiri::XML::Text:0xbd4020 "\n      ">, #<Nokogiri::XML::Element:0xbd3cd8 name="start-date" children=[#<Nokogiri::XML::Text:0xbd3724 "\n        ">, #<Nokogiri::XML::Element:0xbd3364 name="year" children=[#<Nokogiri::XML::Text:0xbd3008 "2012">]>, #<Nokogiri::XML::Text:0xbd27c0 "\n        ">, #<Nokogiri::XML::Element:0xbd2518 name="month" children=[#<Nokogiri::XML::Text:0xbd89cc "8">]>, #<Nokogiri::XML::Text:0xbd8530 "\n      ">]>, #<Nokogiri::XML::Text:0xbd7fa4 "\n      ">, #<Nokogiri::XML::Element:0xbd7e28 name="is-current" children=[#<Nokogiri::XML::Text:0xbd7a90 "true">]>, #<Nokogiri::XML::Text:0xbd7554 "\n      ">, #<Nokogiri::XML::Element:0xbd7324 name="company" children=[#<Nokogiri::XML::Text:0xbd6fb4 "\n        ">, #<Nokogiri::XML::Element:0xbd6dd4 name="id" children=[#<Nokogiri::XML::Text:0xbda9fc "260344">]>, #<Nokogiri::XML::Text:0xbda63c "\n        ">, #<Nokogiri::XML::Element:0xbda470 name="name" children=[#<Nokogiri::XML::Text:0xbd9c50 "Torrid Networks Private Limited">]>, #<Nokogiri::XML::Text:0xbd94d0 "\n        ">, #<Nokogiri::XML::Element:0xbd92f0 name="size" children=[#<Nokogiri::XML::Text:0xbdd47c "11-50 employees">]>, #<Nokogiri::XML::Text:0xbdcf40 "\n        ">, #<Nokogiri::XML::Element:0xbdcd74 name="type" children=[#<Nokogiri::XML::Text:0xbdc978 "Privately Held">]>, #<Nokogiri::XML::Text:0xbdbf28 "\n        ">, #<Nokogiri::XML::Element:0xbdbc44 name="industry" children=[#<Nokogiri::XML::Text:0xbe2710 "Computer & Network Security">]>, #<Nokogiri::XML::Text:0xbe2364 "\n      ">]>, #<Nokogiri::XML::Text:0xbe1dec "\n    ">]>, @company=#<LinkedIn::Company:0x00000001762a68 @doc=[#<Nokogiri::XML::Element:0xbd7324 name="company" children=[#<Nokogiri::XML::Text:0xbd6fb4 "\n        ">, #<Nokogiri::XML::Element:0xbd6dd4 name="id" children=[#<Nokogiri::XML::Text:0xbda9fc "260344">]>, #<Nokogiri::XML::Text:0xbda63c "\n        ">, #<Nokogiri::XML::Element:0xbda470 name="name" children=[#<Nokogiri::XML::Text:0xbd9c50 "Torrid Networks Private Limited">]>, #<Nokogiri::XML::Text:0xbd94d0 "\n        ">, #<Nokogiri::XML::Element:0xbd92f0 name="size" children=[#<Nokogiri::XML::Text:0xbdd47c "11-50 employees">]>, #<Nokogiri::XML::Text:0xbdcf40 "\n        ">, #<Nokogiri::XML::Element:0xbdcd74 name="type" children=[#<Nokogiri::XML::Text:0xbdc978 "Privately Held">]>, #<Nokogiri::XML::Text:0xbdbf28 "\n        ">, #<Nokogiri::XML::Element:0xbdbc44 name="industry" children=[#<Nokogiri::XML::Text:0xbe2710 "Computer & Network Security">]>, #<Nokogiri::XML::Text:0xbe2364 "\n      ">]>]>>]>  
请帮助我!!

1 个答案:

答案 0 :(得分:0)

尝试

user = client.profile(:fields => %w(first_name))
user.first_name

如果您想要更多字段,请检查您从LinkedIn获取的对象

x.class # To see where to find documentation about the object

似乎x是LinkedIn::Mash < Hashie::Mash,然后您可以通过@doc中的某种方法访问x实例变量,出于测试目的,您可以执行此操作:

x.instance_eval { p @doc.to_xml }