如何在rails上的ruby中的字段<event> <payload> <company> <name>中检索数据

时间:2018-01-23 06:18:46

标签: xml ruby-on-rails-3 nokogiri

如何在轨道上的ruby中的字段<event><payload><company><name>中检索数据?

tempvariable contains the below xml data:

<event xmlns:atom="http://www.w3.org/2005/Atom">
<type>SUBSCRIPTION_ORDER</type>
<marketplace>
<baseUrl>https://acme.appdirect.com</baseUrl>
<partner>ACME</partner>
</marketplace>
<flag>STATELESS</flag>
<creator>
<email>test-email+creator@appdirect.com</email>
<firstName>DummyCreatorFirst</firstName>
<language>fr</language>
<lastName>DummyCreatorLast</lastName>
<openId>https://www.appdirect.com/openid/id/ec5d8eda-5cec-444d-9e30-125b6e4b67e2</openId>
<uuid>ec5d8eda-5cec-444d-9e30-125b6e4b67e2</uuid>
</creator>
<payload>
<company>
<country>CA</country>
<email>company-email@example.com</email>
<name>Example Company Name</name>
<phoneNumber>415-555-1212</phoneNumber> 
<uuid>d15bb36e-5fb5-11e0-8c3c-00262d2cda03</uuid>
</company>
</payload>
<returnUrl>https://www.appdirect.com/finishprocure?token=dummyOrder</returnUrl>
</event>

1 个答案:

答案 0 :(得分:1)

tempvariable = request_from_marketplace(params["eventUrl"])
doc = Nokogiri::XML(tempvariable)
doc.xpath('//company').each do |char_element|
  puts char_element.xpath('name').try(:text)
end