Parsing a SOAP response using libxml in Ruby

I am trying to parse the following SOAP answer 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 something inside the tag and the value of connectionHandlerID.

+4
source share
2 answers

When you use Savon, you can convert the response to a hash. The response.to_hash conversion method also does some other useful things for you.

Then you can get the desired value using a 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

+6
source

I would recommend nokogiri .

Assuming your XML response is in an object called response.

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

Source: https://habr.com/ru/post/1308147/


All Articles