我是Ruby on Rails的新手,並且正在使用Savon連接到SOAP服務。我得到如下回應:從Savon XML響應中獲取價值?
<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<GetInformationsForCoordinatesResponse xmlns="http://tempuri.org/">
<GetInformationsForCoordinatesResult>ABC14522</GetInformationsForCoordinatesResult>
</GetInformationsForCoordinatesResponse>
</s:Body>
</s:Envelope>
我需要得到的GetInformationsForCoordinatesResult
值,即ABC14522
。
從莎翁的文檔,我知道,薩翁用紫菜和引入nokogiri。在我的腳本中我包含了require 'nokogiri'
。
result = response.xpath("//GetInformationsForCoordinatesResult").first.inner_text
然而,當我跑我得到了錯誤代碼:
NoMethodError: undefined method `inner_text' for nil:NilClass
有人能指出這可能是錯在這裏或者表現出更好的方法來提取我從響應所需的值?
你可以使用一個引入nokogiri或者,這是響應對象上的薩翁to_hash。你可以簡單地遍歷它(雖然我不記得它開始的級別)。響應[:Envelope] [:Body] [:Get ... Response] [:Get ... Result] –
您應該使用Savon的Response對象方法。你不需要用Nokogiri推出自己的產品 - Savon的Response對象會自動委託給它。另外,不需要它。 –
@MarkThomas - 好的,謝謝 - 我才意識到我可以叫response.doc的響應,然後我就能夠使用從回答下面的代碼讓我的價值 –