cLee cLee - 3 months ago 10
Ruby Question

how to parse repeated elements with nokogiri

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="6181e65d-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>

Answer

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