0

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 parent of the current node.

Colen
  • 13,428
  • 21
  • 78
  • 107

2 Answers2

1
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> ... </>
Vasiliy Ermolovich
  • 24,459
  • 5
  • 79
  • 77
  • Oh... now why isn't that listed in the docs here - http://www.ruby-doc.org/stdlib/libdoc/rexml/rdoc/classes/REXML/Element.html ? :( – Colen Jul 14 '11 at 20:34
0

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
Ram
  • 991
  • 7
  • 8