Get immediate parent node in REXML
How do I get the immediate parent of a node with REXML? root_node() gets me the parent node of the document, but I just want the pa开发者_JAVA百科rent of the current node.
require "rexml/document"
string = "
<root>
<a>
<b>
test
</b>
</a>
</root>"
doc = REXML::Document.new string
p doc[1][1][1] #=> <b> ... </>
p doc[1][1][1].parent #=> <a> ... </>
If you know the element, then you can achieve this by following set of lines:
doc.get_elements('//your_element_name')[0].parent
From above example it will be like:
doc.get_elements('//b')[0].parent
精彩评论