如何遍历NodeList?

YangWenChaoX 2009-06-19 04:01:44
如题,注意:并不一定只有一层循环,不知道节点下面有没有子节点,也不知道子节点下面还有没有子节点...? 谢谢,重金有赏!
...全文
1437 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
xmShengmin 2012-06-29
  • 打赏
  • 举报
回复
public void traver(Node node){
if(node instanceof TagNode){
System.out.println(((TagNode)node).getText());
}

Node child=node.getFirstChild();
while(child!=null){
traver(child);
child=child.getSibling();
}


}
link333 2011-07-21
  • 打赏
  • 举报
回复
学习一下~~!
zou118627 2009-06-19
  • 打赏
  • 举报
回复
没有实际的应用,怎么给实例啊。。
自己看看,稍微改改就好了的,楼主
YangWenChaoX 2009-06-19
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 lsxjl 的回复:]
我也认为该使用递归的方法 深度优先搜索。 这和搜索一个文件夹下面的所有文件差不多。
[/Quote]

递归,
哪位高手能给出可以运行的代码吗? 我愿意出100分。
zm_hs 2009-06-19
  • 打赏
  • 举报
回复
递归
linuxlsx 2009-06-19
  • 打赏
  • 举报
回复
我也认为该使用递归的方法 深度优先搜索。 这和搜索一个文件夹下面的所有文件差不多。
YangWenChaoX 2009-06-19
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wind_rosebush 的回复:]
Java code
// 用递归方法遍历DOM树
public void GetElement(NodeList nodelist) {
Node cnode;
int i;
String str;
int len;

if (nodelist.getLength() == 0) {
return;
}
for (i = 0; i < nodelist.getLength(); i++) {
cnode = nodelist.item(i);
if (cnode.getNodeType() == 1) {
if(c…
[/Quote]
大哥,能加注释吗?看的好吃力!
wind_rosebush 2009-06-19
  • 打赏
  • 举报
回复

// 用递归方法遍历DOM树
public void GetElement(NodeList nodelist) {
Node cnode;
int i;
String str;
int len;

if (nodelist.getLength() == 0) {
return;
}
for (i = 0; i < nodelist.getLength(); i++) {
cnode = nodelist.item(i);
if (cnode.getNodeType() == 1) {
if(cnode.hasChildNodes()){
NodeList childlist = cnode.getChildNodes();
if(childlist.item(i).getNodeType()==Node.ENTITY_NODE){
String stringLetter=cnode.getNodeName().substring(0, 1).toUpperCase();
String setName="set"+ stringLetter + cnode.getNodeName().substring(1);
// Method setMethod=fs.getMethod(setName, cnode.getNodeType());
}
}
GetElement(cnode.getChildNodes());
} else if (cnode.getNodeType() == 3) {
str = cnode.getNodeValue();
len = str.length();
if (len > 1){
String fieldName = cnode.getParentNode().getNodeName();
String stringLetter=fieldName.substring(0, 1).toUpperCase();
String setName="set"+stringLetter+fieldName.substring(1);
Field[] fields1=head.getDeclaredFields();
Field[] fields2=msg.getDeclaredFields();
for (int j = 0; j < fields1.length; j++) {
if(fields1[j].getName()==fieldName){
try {
Method setMethod = head.getMethod(setName, fields1[j].getType().getClass());
setMethod.invoke(head, str);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

for (int j = 0; j < fields2.length; j++) {
if(fields2[j].getName()==fieldName){
try {
Method setMethod = msg.getMethod(setName, fields2[j].getType().getClass());
setMethod.invoke(msg, str);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
}

// 遍历所提供的DOM文档.
public void ParseDom(Document document) {
NodeList nodelist;

Element element = document.getDocumentElement();
// System.out.println("----" + element.getNodeName());

nodelist = element.getChildNodes();
GetElement(nodelist);
}

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧