tags:

views:

210

answers:

2

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/"&gt;
    <soapenv:Body>
        <ns:getConnectionResponse xmlns:ns="http://webservice.jchem.chemaxon"&gt;
            <ns:return>
                &lt;ConnectionHandlerId>connectionHandlerID-283854719&lt;/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.

+2  A: 

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
thanks Crudson, I am looking on nokogiri. But there are some nokogiri issues with JRuby which I am currently using.
abhishektiwari
+2  A: 

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

Steve Weet