Searching savon response as nokogiri document returns an empty array

女生的网名这么多〃 提交于 2019-12-25 03:19:25

问题


I try to parse savon's response as nokokiri document

c = Savon.client(wsdl: 'http://test.fedresurs.ru/MessageService/WebService.svc?wsdl', digest_auth: ['demowebuser', 'Ax!761BN'], namespace: "http://tempuri.org/", namespace_identifier: :tem, log: true)
r = c.call(:get_trade_messages, message: {'tem:startFrom' => DateTime.now-1})
r.doc.search("TradePlace")

and it returns an empty array.

What I'm doing wrong? May be I should deal somehow with namespaces? But, how?. Examples, that I found in nokogiri documentation use Xpath, not search. And even with Xpath it returns an empty array.

XML-response:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <GetTradeMessagesResponse xmlns="http://tempuri.org/">
         <GetTradeMessagesResult>
            <TradePlace INN="7606055642" Name="Первая электронная площадка " Site="1torgi.ru " OwnerName="ООО &quot;Промтех&quot;">
               <TradeList>
                  <Trade ID_External="ЗКОФЦП-17136" ID_EFRSB="653476">
                     <MessageList>
                        <TradeMessage ID="4851134"/>
                        <TradeMessage ID="4851135"/>
                     </MessageList>
                  </Trade>
               </TradeList>
            </TradePlace>
         </GetTradeMessagesResult>
      </GetTradeMessagesResponse>
   </s:Body>
</s:Envelope>

回答1:


You can use Nokogiri to break apart the XML response. A (now nofunctional) example is this:

doc = Nokogiri::XML(response.to_hash[:get_quote_response][:get_quote_result])
print doc.to_xml(indent: 2)

print "Date      : ", doc.at_css("Date").text, "\n"
print "Last price: ", doc.at_css("Last").text

are more complete example in my pastebin https://pastebin.com/W0RUuaHU. The WebserviceX was unfortunately discontinued.




回答2:


As I expected the answer was in namespace, code below works fine:

r.doc.search("a|TradePlace", {"a" => "http://tempuri.org/"})


来源:https://stackoverflow.com/questions/52886666/searching-savon-response-as-nokogiri-document-returns-an-empty-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!