java删除xml节点的问题

[复制链接]
查看11 | 回复3 | 2010-6-17 10:57:28 | 显示全部楼层 |阅读模式

style
null
style


style
null
style


style
null
style


style
null
style


style
null
style




Help
control H
help


About JEditor
null
about


要求删除下的所有,但保留,只能使用Node, NodeList, Element完成。请指教!!!!!
NodeList menus = menuBarXML.getElementsByTagName("menu");
for (int i = 0; i != menus.getLength(); ++i) {
Element me = (Element) menus.item(i);
if ("Style".equals(me.getAttribute("name"))) {

NodeList menuItems = me.getChildNodes();

System.out.println(menuItems.getLength());

for (int j = 0; j != menuItems.getLength(); ++j) {

me.removeChild(menuItems.item(j));

}

break;
}
}
我写了这段代码,但是不能正常工作。。。空指针
而且我不明白输出那句为什么是11?

回复

使用道具 举报

千问 | 2010-6-17 10:57:28 | 显示全部楼层
把: for (int i = 0; i != menus.getLength(); ++i) {改成: for (int i = 0; i 节点,然后有两种方法:1、调用menu.parentNode的removeChild,删除menu节
回复

使用道具 举报

千问 | 2010-6-17 10:57:28 | 显示全部楼层
NodeList menus = menuBarXML.getElementsByTagName("menu");for (int i = 1; i <menus.getLength(); i+=2) { Element me = (Element) menus.item(i); if ("Style".equals(me.getAt
回复

使用道具 举报

千问 | 2010-6-17 10:57:28 | 显示全部楼层
想问楼上,这样改不改有什么不同?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行