long id=0
                        Element rootsel=doc.getRootElement().getChild("treesel");
List<Element> selist=rootsel.getChildren("tree");
for(Iterator<Element> iter=selist.iterator();iter.hasNext();){
Element el=iter.next();
if(Long.parseLong(el.getAttributeValue("id"))==id){
selist.remove(el);
                                       }
}
代码如上,我的xml文档有好几个id=0的节点,我想删除所有id=0的节点,但是一删就清空了,为什么?

解决方案 »

  1.   

    我查看了一下Element接口的 API,好像没有getAttributeValue()方法啊
    方法摘要 
     String getAttribute(String name) 
              通过名称获得属性值。 
     Attr getAttributeNode(String name) 
              通过名称获得属性节点。 
     Attr getAttributeNodeNS(String namespaceURI, String localName) 
              通过本地名称和名称空间 URI 获得 Attr 节点。 
     String getAttributeNS(String namespaceURI, String localName) 
              通过本地名称和名称空间 URI 获得属性值。 你可以使用getAttribute("id")==id;并且你需要用个循环来遍历selist中的其他的属性值来进行删除,不然你上面只是针对第一个属性值来判断的,
    其他的都没有遍历
      

  2.   

    有这个el.getAttributeValue("id"),我都用它读出数据了,直接用eclipse就能有提示的.