I have the following XML consumed from a REST API:
<dataitems>
<dataitem colour="null">
<value>
<label>Intel</label>
<count>43</count>
</value>
<value>
<label>AMD</label>
<count>39</count>
</value>
<value>
<label>ARM</label>
<count>28</count>
</value>
</dataitem>
</dataitems>
I would like to search for the text in the <label> tag and display the matching value for <count> in a table.
In the controller I have: @post_count = Nokogiri::XML(Post.item_data.to_xml).
In the view I’m not sure whether I need to use @post_count.xpath or @post_count.search.
Can someone please point me in the right direction on the right method and syntax for it?
Thanks in advance.
Although I’m not sure what information you are looking for, I have a few suggestions.
1) If you know the value in the element before you do your searching:
2) Assuming that you don’t know the names within the tag in advance
I personally like the second solution better because it gives you a clean hash, but I prefer to work with hashes and arrays as quickly as possible.