I'm new to Nokogiri, so how do I parse the "data" and it's text, as well as the "name" from the "method" in the following xml:
<get_escalators_response status="200" status_text="OK">
<escalator id="6开发者_Python百科181e65d-8ba0-4937-9c44-8f2b10b0def7">
<name>Team alert</name>
<comment/>
<in_use>1</in_use>
<condition>
Threat level at least
<data>
High
<name>level</name>
</data>
</condition>
<event>
Task run status changed
<data>
Done
<name>status</name>
</data>
</event>
<method>
Email
<data>
team@example.org
<name>to_address</name>
</data>
<data>
admin@example.org
<name>from_address</name>
</data>
<data>
0
<name>notice</name>
</data>
</method>
</escalator>
...
</get_escalators_response>
Assigning your XML to a variable called xml
, I'd go about it like this:
require 'nokogiri'
require 'pp'
doc = Nokogiri::XML(xml)
pp doc.search('//method/data').map{ |n| n.text.scan(/\S+/) }
Notice this is returning an array of arrays. It'd be easy to coerce the data into strings or hashes.
# >> [["team@example.org", "to_address"],
# >> ["admin@example.org", "from_address"],
# >> ["0", "notice"]]
There's several ways to do this, here's one:
doc = Nokogiri::XML("your_xml_document")
doc.search("data").each do |data|
name = data.search("name").remove # remove the name element from data element
name_text = name.text
data_text = data.text
# do stuff with text
end
You can search for specific nested elements like this:
doc.search("method > data").each do |data|
# do stuff
end
精彩评论