Get the first element and first child : XPath « XML « Ruby






Get the first element and first child


xml = %{
<aquarium>
 <fish color="blue" size="small" />
 <fish color="orange" size="large">
   <fish color="green" size="small">
    <fish color="red" size="tiny" />
   </fish>
 </fish>
 <decoration type="castle" style="gaudy">
  <algae color="green" />
 </decoration>
</aquarium>}

require 'rexml/document'
doc = REXML::Document.new xml


doc.elements[1]
# => <aquarium> ... </>
doc.children[0]
# => <aquarium> ... </>

 








Related examples in the same category

1.Get XPath from node
2.Pass xml node to a method
3.Navigating a Document with XPath
4.REXML::XPath.match(doc, '//[@color="green"]')
5.Find the second green element.
6.Find the color attributes of all small fish.
7.Count how many fish are inside the large fish.
8.each loop on element
9.Search element by XPath