public void delNode(String listName, String nodeName) throws XMLException, DocumentException, IOException{
String filePath = ".....";
String path = filePath + listName + ".xml";
File file = checkAndLoad(path);

SAXReader read = new SAXReader();
Document doc = read.read(file);
Element e = doc.getRootElement();
List<Element> list = e.elements(nodeName);

for(Iterator<Element> it = list.iterator(); it.hasNext();){
if(it.next().attributeValue("name").equalsIgnoreCase(nodeName)){
it.remove();
}
}
saveAndClose(doc, file);
}
为什么删不掉呢?????

解决方案 »

  1.   


    List   DROList   =   getRootElement().elements(); for(int   i   =0;i <DROList.size();i++){ Element   database   =   (Element)DROList.get(i); 
    Element   e_name   =   database.element( "name ");  Element   e_modifytime   =   database.element( "modifytime "); 
     if(e_name.getText().equals(xmlForm.getName())){  DROList.remove(i); 

                            
        

    saveDocument(); 
    希望对你有帮助
      

  2.   


    List<Element> list = e.elements(nodeName);for(Iterator<Element> it = list.iterator(); it.hasNext();){
        if(it.next().attributeValue("name").equalsIgnoreCase(nodeName)){
        it.remove();
    }
    楼主,这里remove,是remove掉list里的Element
    xml生成的各结点放在一个list中,remove的时候是remove这个list,而不是remove xml文件的结点。
      

  3.   

    刚说的有点问题。
    对list操作是对xml 文件的操作。
    但remove的时候,不要用list.remove。用 e.remove(it.next())
      

  4.   

    用e.remove(it.next())会报NoSuchElementException..
    要先Element ee = it.next();再e.remove(ee);
    就ok..