在Ruby中使用libxml解析SOAP响应

时间:2010-04-27 15:18:43

标签: ruby soap xmp

我正在尝试解析来自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而没有任何成功。基本上我想在tag和connectionHandlerID值中提取任何内容。

2 个答案:

答案 0 :(得分:6)

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

然后,您可以使用类似于以下内容的代码获取所需的值

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

查看documentation

答案 1 :(得分:2)

我建议nokogiri

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

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