Use nokogiri
and css
selector:
xml = Nokogiri::XML(open("http://www.mywebsite.com/awesome_xml.xml"))
xml.css("activity[type='module']")
Question
Say I'm loading an XML as so :
url = "http://www.mywebsite.com/awesome_xml.xml"
xml = HTTParty.get url, format: :xml
And my XML looks like this :
<activity id="6RhUUfdP8b1" type="objective">
<name lang="und">Results</name>
</activity>
<activity id="6bKsxwZaTjT" type="module">
<name lang="und">Wild and whacky?</name>
</activity>
And I want to collect all the <activity>
nodes that have a type equal to module
.
What is the most elegant way to perform this?
Solution
Use nokogiri
and css
selector:
xml = Nokogiri::XML(open("http://www.mywebsite.com/awesome_xml.xml"))
xml.css("activity[type='module']")
OTHER TIPS
Use XPath:
//activity[@type='module']