开发者

Parsing SOAP response using libxml in Ruby

I am trying to parse following SOAP response coming from Savon SOAP api

<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv开发者_C百科="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>

I am trying to use libxml-ruby without any success. Basically I want to extract anything inside tag and the connectionHandlerID value.


As you are using Savon you can convert the response to a hash. The conversion method response.to_hash does some other useful things for you as well.

You would then be able to get the value you want using code similar to the following

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

Check out the documentation


I'd recommend nokogiri.

Assuming your XML response is in an object named response.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜