user178841
user178841

Reputation:

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="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.

Upvotes: 4

Views: 2920

Answers (2)

Steve Weet
Steve Weet

Reputation: 28402

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

Upvotes: 5

user253455
user253455

Reputation: 465

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

Upvotes: 2

Related Questions