문제

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?

도움이 되었습니까?

해결책

Use nokogiri and css selector:

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

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

다른 팁

Use XPath:

//activity[@type='module']
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top