4

我正在尝试解析来自 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 值。

4

2 回答 2

5

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

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

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

查看文档

于 2010-04-29T06:46:15.567 回答
2

我会推荐nokogiri

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

require 'nokogiri'
doc = Nokogiri::XML::parse response
doc.at_xpath("//ConnectionHandlerId").text
于 2010-04-27T19:01:04.443 回答