当前位置: 首页 > 工具软件 > dom4j > 使用案例 >

dom4j的org.dom4j.IllegalAddException处理

亢琦
2023-12-01
org.dom4j.IllegalAddException: The node "your node description..." could not be added to the element "yourNode" because: The Node already has an existing parent of "yourNode" 


Document  doc = DocumentHelper.createDocument();

Element   root = doc.addElement("config")

Element   files = root.addElement("files");

String configFiles="<files><file name="AdShower123.exe" oriname="update\adshower123.zip" ver="1.0.0.1" md5="" update="http://soft.51cy.org:80/adv/package/adshower.zip" action="3" param="" op="0" optype="3" opcmd="$(appdir)\AdShower.exe" opparam=""/><file name="AdShower123.exe" oriname="update\adshower123.zip" ver="1.0.0.1" md5="" update="http://soft.51cy.org:80/adv/package/adshower.zip" action="3" param="" op="0" optype="3" opcmd="$(appdir)\AdShower.exe" opparam=""/></files>";


Document fileDom=DocumentHelper.parseText(configFiles);
//root.add(fileDom.getRootElement());
//System.out.println("2222222222:"+fileDom.selectNodes("files").toString());
for (Iterator iterator = fileDom.getRootElement().elementIterator(); iterator.hasNext();) {
  Element e = (Element) iterator.next();
  // files.add(e);
 files.add((Element)e.clone());
}

红色是出错的位置,蓝色为正确的写法

参考链接:http://moon-cake.iteye.com/blog/298052

 类似资料: