开发者

How to Transver back up a tree from a sibling Node

开发者 https://www.devze.com 2023-01-20 22:47 出处:网络
I have a transverse tree written in JSP which goes through an XML file. When I get to a certain Text Node, I\'d like to be able to search back up the tree to find a certain element associated with th

I have a transverse tree written in JSP which goes through an XML file.

When I get to a certain Text Node, I'd like to be able to search back up the tree to find a certain element associated with that node.

I'm 开发者_如何学Cthinking I need to do a For loop and use some kind of 'getLastNode' or 'getParentNode' function. Would this be the correct method? I'm a little unsure of the syntax, so any help would be much appreciated!

I did a bit of search and I can't find anything which demonstrates what I'm trying to do nor can I find a list of the functions I'm after.


You need to keep calling getParentNode until you hit a node matching your criteria. For example:

public Node searchUpFor(String tagToFind, Node aNode) {
    Node n = aNode.getParentNode();
    while (n != null && !n.getNodeName().equals(tagToFind)) {
        n = n.getParentNode();
    }
    return n;
}
0

精彩评论

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