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 performing a search:
doc = Nokogiri.XML(open(source_xml)) # Assuming there is only one of each label node = doc.xpath('//label[text()="Intel"]').first count = node.next_element.text # or if there are many of each label nodes = doc.xpath('//label[text()="Intel"]') nodes.each {|node| count = node.next_element.text # do something with count here }
2) Assuming you donโt know the names in the tag in advance
doc = Nokogiri.XML(open(source_xml)) labels = {} doc.xpath('//label').each {|node| labels[node.text] = node.next_element.text } # labels => {"Intel"=>"43", "AMD"=>"39", "ARM"=>"28"}
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.
source share