【问题标题】:Parsing SOAP response using libxml in Ruby在 Ruby 中使用 libxml 解析 SOAP 响应
【发布时间】:2010-04-27 15:18:42
【问题描述】:

我正在尝试解析来自 Savon SOAP api 的以下 SOAP 响应

<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
        <ns:getConnectionResponse xmlns:ns="http://webservice.jchem.chemaxon">
            <ns:return>
                <ConnectionHandlerId>connectionHandlerID-283854719</ConnectionHandlerId>
            </ns:return>
        </ns:getConnectionResponse>
    </soapenv:Body>
</soapenv:Envelope>

我正在尝试使用 libxml-ruby,但没有成功。基本上我想提取标签内的任何内容和 connectionHandlerID 值。

【问题讨论】:

    标签: ruby soap xmp


    【解决方案1】:

    当您使用 Savon 时,您可以将响应转换为哈希。转换方法response.to_hash 还为您做了一些其他有用的事情。

    然后您将能够使用类似于以下的代码获取所需的值

    hres = soap_response.to_hash
    conn_handler_id = hres[:get_connection_response][:return][:connection_handler_id]
    

    查看documentation

    【讨论】:

      【解决方案2】:

      我推荐nokogiri

      假设您的 XML 响应位于名为 response 的对象中。

      require 'nokogiri'
      doc = Nokogiri::XML::parse response
      doc.at_xpath("//ConnectionHandlerId").text
      

      【讨论】:

      • 感谢 Crudson,我正在寻找 nokogiri。但是我目前正在使用的 JRuby 存在一些 nokogiri 问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-26
      • 1970-01-01
      • 1970-01-01
      • 2020-04-22
      • 1970-01-01
      相关资源
      最近更新 更多