常常需要在两个Document中互相复制Element,可是Dom4j中使用Element.add(Elemnet)方法就会出现出题的错误:
而应用AppendContext()方法,只能将目标元素的内容复制过来,不能将整个元素复制
通过看AbstractElement.java的源码得到解决办法是:调用Element的clone()方法。
root.add((Element) company.clone());
public Document createXMLDocument(){
Document doc = null;
doc = DocumentHelper.createDocument();
Element root = doc.addElement("class");
Element company = root.addElement("company");
Element person = company.addElement("person");
person.addAttribute("id","11");
person.addElement("name").setText("Jack Chen");
person.addElement("sex").setText("男");
person.addElement("date").setText("2001-04-01");
person.addElement("email").setText("chen@163.com");
person.addElement("QQ").setText("2366001");
root.add((Element) company.clone());
return doc;
}
来自:http://hi.baidu.com/yangtb/blog/item/ca48e9ea2c3e4ed0d439c995.html
==========================================================
在xml开发中经常会需要将一个Document的部分元素复制到另一个Document中,但dom4j中直接add会产生“The Node already has an existing parent”异常。试了一下,有两种方法可以添加。
Document oldDoc = new SAXReader().read(new FileInputStream("file.xml")); |