如何从xml响应中获取/检索id

时间:2013-11-30 17:21:37

标签: ruby-on-rails xml ruby-on-rails-3 savon

我正在使用savon gem来获取xml信息。

xml响应就像这样

"<?xml version='1.0' encoding='UTF-8'?><S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\"><S:Body><Divisions xmlns=\"urn:description7a.services.chrome.com\"><responseStatus responseCode=\"Successful\" description=\"Successful\"/><division id=\"1\">Acura</division><division id=\"44\">Aston Martin</division><division id=\"4\">Audi</division><division id=\"45\">Bentley</division><division id=\"5\">BMW</division><division id=\"6\">Buick</division><division id=\"7\">Cadillac</division><division id=\"8\">Chevrolet</division><division id=\"9\">Chrysler</division><division id=\"11\">Dodge</division><division id=\"46\">Ferrari</division><division id=\"59\">FIAT</division><division id=\"13\">Ford</division><division id=\"15\">GMC</division><division id=\"16\">Honda</division><division id=\"17\">Hyundai</division><division id=\"18\">Infiniti</division><division id=\"19\">Isuzu</division><division id=\"20\">Jaguar</division><division id=\"21\">Jeep</division><division id=\"22\">Kia</division><division id=\"47\">Lamborghini</division><division id=\"23\">Land Rover</division><division id=\"24\">Lexus</division><division id=\"25\">Lincoln</division><division id=\"48\">Lotus</division><division id=\"53\">Maserati</division><division id=\"26\">Mazda</division><division id=\"27\">Mercedes-Benz</division><division id=\"2\">MINI</division><division id=\"29\">Mitsubishi</division><division id=\"30\">Nissan</division><division id=\"34\">Porsche</division><division id=\"57\">Ram</division><division id=\"49\">Rolls-Royce</division><division id=\"52\">Scion</division><division id=\"42\">Smart</division><division id=\"37\">Subaru</division><division id=\"38\">Suzuki</division><division id=\"56\">Tesla</division><division id=\"39\">Toyota</division><division id=\"40\">Volkswagen</division><division id=\"41\">Volvo</division></Divisions></S:Body></S:Envelope>"

我想分割id并创建名称。

我该怎么做

被修改

我已将xml保存在像

这样的变量中
  xml = 1.9.3p194 :063 > xml
 => "<?xml version='1.0' encoding='UTF-8'?><S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\"><S:Body><Divisions xmlns=\"urn:description7a.services.chrome.com\"><responseStatus responseCode=\"Successful\" description=\"Successful\"/><division id=\"1\">Acura</division><division id=\"44\">Aston Martin</division><division id=\"4\">Audi</division><division id=\"45\">Bentley</division><division id=\"5\">BMW</division><division id=\"6\">Buick</division><division id=\"7\">Cadillac</division><division id=\"8\">Chevrolet</division><division id=\"9\">Chrysler</division><division id=\"11\">Dodge</division><division id=\"46\">Ferrari</division><division id=\"59\">FIAT</division><division id=\"13\">Ford</division><division id=\"15\">GMC</division><division id=\"16\">Honda</division><division id=\"17\">Hyundai</division><division id=\"18\">Infiniti</division><division id=\"19\">Isuzu</division><division id=\"20\">Jaguar</division><division id=\"21\">Jeep</division><division id=\"22\">Kia</division><division id=\"47\">Lamborghini</division><division id=\"23\">Land Rover</division><division id=\"24\">Lexus</division><division id=\"25\">Lincoln</division><division id=\"48\">Lotus</division><division id=\"53\">Maserati</division><division id=\"26\">Mazda</division><division id=\"27\">Mercedes-Benz</division><division id=\"2\">MINI</division><division id=\"29\">Mitsubishi</division><division id=\"30\">Nissan</division><division id=\"34\">Porsche</division><division id=\"57\">Ram</division><division id=\"49\">Rolls-Royce</division><division id=\"52\">Scion</division><division id=\"42\">Smart</division><division id=\"37\">Subaru</division><division id=\"38\">Suzuki</division><division id=\"56\">Tesla</division><division id=\"39\">Toyota</division><division id=\"40\">Volkswagen</division><division id=\"41\">Volvo</division></Divisions></S:Body></S:Envelope>" 

我试过

 @doc = Nokogiri::XML(xml)

它提供了类似

的内容
#<Nokogiri::XML::Document:0x5ea65f0 name="document" children=[#<Nokogiri::XML::Element:0x5ea6398 name="Envelope" namespace=#<Nokogiri::XML::Namespace:0x5ea6370 prefix="S" href="http://schemas.xmlsoap.org/soap/envelope/"> children=[#<Nokogiri::XML::Element:0x5ea5fc4 name="Body" namespace=#<Nokogiri::XML::Namespace:0x5ea6370 prefix="S" href="http://schemas.xmlsoap.org/soap/envelope/"> children=[#<Nokogiri::XML::Element:0x5ea5c5e name="Divisions" namespace=#<Nokogiri::XML::Namespace:0x5ea5c18 href="urn:description7a.services.chrome.com"> children=[#<Nokogiri::XML::Element:0x5ea592a name="responseStatus" namespace=#<Nokogiri::XML::Namespace:0x5ea5c18 href="urn:description7a.services.chrome.com"> attributes=[#<Nokogiri::XML::Attr:0x5ea5858 name="responseCode" value="Successful">, #<Nokogiri::XML::Attr:0x5ea581c name="description" value="Successful">]>, #<Nokogiri::XML::Element:0x5ea518c name="division" namespace=#<Nokogiri::XML::Namespace:0x5ea5c18 href="urn:description7a.services.chrome.com"> attributes=[#<Nokogiri::XML::Attr:0x5ea515a name="id" value="1">] children=[#<Nokogiri::XML::Text:0x5ea4ad4 "Acura">]>, #<Nokogiri::XML::Element:0x5ea491c name="division" namespace=#<Nokogiri::XML::Namespace:0x5ea5c18 href="urn:description7a.services.chrome.com"> attributes=[#<Nokogiri::XML::Attr:0x5ea489a name="id" value="44">] children=[#<Nokogiri::XML::Text:0x63dc434 "Aston Martin">]>, #<Nokogiri::XML::Element:0x63dc222 name="division" namespace=#<Nokogiri::XML::Namespace:0x5ea5c18 href="urn:description7a.services.chrome.com"> attributes=[#<Nokogiri::XML::Attr:0x63dc1f0 name="id" value="4">] children=[#<Nokogiri::XML::Text:0x63dbcbe "Audi">]>, #<Nokogiri::XML::Element:0x63dbb56 name="division" namespace=#<Nokogiri::XML::Namespace:0x5ea5c18 href="urn:description7a.services.chrome.com"> attributes=[#<Nokogiri::XML::Attr:0x63dbb10 name="id" value="45">] ......
我试过了      @ doc.xpath('// S:Envelope // S :: Body')。每个都做| f |        把f      端

我像

一样出局
<S:Body>
  <Divisions xmlns="urn:description7a.services.chrome.com">
  <responseStatus responseCode="Successful" description="Successful"/>
    <division id="1">Acura</division>
    <division id="44">Aston Martin</division>
    <division id="4">Audi</division>
    <division id="45">Bentley</division>
    <division id="5">BMW</division>
    .......
  </Divisions>
</S:Body>

我想检索id和名称等信息 所以我试过

它返回[],空数组

 @doc.xpath('//S:Envelope//S::Body//Divisions')

它返回空数组,如[]

2 个答案:

答案 0 :(得分:0)

我相信你应该看看Nokogiri和XPAth。使用XML时,XPath是非常强大的工具。 Here您可以找到使用Nokogiri + XPath的示例。你应该阅读documentation about xpath(借鉴上一个链接:))。当然,你可以在google中找到很多其他的例子。

PS恕我直言XPath是强大但有趣的逻辑(概念),我怀疑你会从第一次尝试中获得好结果,但值得花时间学习它。

答案 1 :(得分:0)

以下是您的xml

<somexml>
  <name>Foo</name>
  <id>1</id>
</somexml>

然后您可以将其加载到irb中,如

require 'rubygems'
require 'nokogiri'
@doc = Nokogiri::XML(File.open("test.xml"))
@doc.xpath("//somexml//id").children.first.to_s

这应该为您提供您正在寻找的ID,请查看上面给出的文档链接以获取更多帮助