steharro
steharro

Reputation: 1079

Parsing Savon XML Response

Here is the response from a SOAP request in SOAPUI

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
  <PostApplication_V5Response xmlns="http://QuiddiServicePricePoints.org/">
     <PostApplication_V5Result><![CDATA[<QuiddiSerivce>
        <Application>
           <Status>PURCHASED</Status>
           <RedirectURL>http://www.google.com?test=abc&xyz=123</RedirectURL>
        </Application>
     </QuiddiSerivce>]]></PostApplication_V5Result>
  </PostApplication_V5Response>
 </soap:Body>
</soap:Envelope>

However when I get the response.body from the Savon request it returns

{:post_application_v5_response=>{:post_application_v5_result=>"<QuiddiSerivce><Application><Status>PURCHASED</Status><RedirectURL>http://www.google.com?test=abc&xyz=123</RedirectURL></Application></QuiddiSerivce>", :@xmlns=>"http://QuiddiServicePricePoints.org/"}}

As you can see, the data I need, namely Status and RedirectURL, our still in XML format.

I've tried to convert [:post_application_v5_response][:post_application_v5_result] to a Hash using Hash.from_xml(), but it fails due to illegal characters in the RedirectURL.

How can I correctly escape characters in the RedirectURL so that I can convert the result to a hash, or otherwise access the Status and RedirectURL?

Upvotes: 0

Views: 2022

Answers (1)

Yevgeniy Anfilofyev
Yevgeniy Anfilofyev

Reputation: 4847

You could use Nokogiri to parse response body. And then search parsed document for desired values.

require 'nokogiri'
response_body = {:post_application_v5_response=>{:post_application_v5_result=>"<QuiddiSerivce><Application><Status>PURCHASED</Status><RedirectURL>http://www.google.com?test=abc&xyz=123</RedirectURL></Application></QuiddiSerivce>", :@xmlns=>"http://QuiddiServicePricePoints.org/"}}
res = Nokogiri.XML(response_body[:post_application_v5_response][:post_application_v5_result])
p res.search('Status').text
p res.search("RedirectURL").text

The result is:

#=> "PURCHASED"
#=> "http://www.google.com?test=abc=123"

Upvotes: 3

Related Questions