如何从同级节点转换备份树 [英] How to Transver back up a tree from a sibling Node
问题描述
我有一棵用JSP编写的横向树,它遍历一个XML文件.
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.
我认为我需要做一个For循环并使用某种"getLastNode"或"getParentNode"功能.这是正确的方法吗?我不太确定语法,因此将不胜感激!
I'm thinking 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.
推荐答案
您需要继续调用getParentNode
,直到找到符合条件的节点为止.例如:
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;
}
这篇关于如何从同级节点转换备份树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!