开发者

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜