Pergunta

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?

Foi útil?

Solução

Use nokogiri and css selector:

xml = Nokogiri::XML(open("http://www.mywebsite.com/awesome_xml.xml"))

xml.css("activity[type='module']")

Outras dicas

Use XPath:

//activity[@type='module']
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top