需要使用 dom4j 处理文档的帮助
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;
public class Main {
public static void main(String[] args){
Company cp17 = new Company();
Person ps1 = new Person("Barry","15900000000");
Person ps2 = new Person("Andy","15800000000");
cp17.employee.add(ps1);
cp17.employee.add(ps2);
Document document = DocumentHelper.createDocument();
Element companyElement = document.addElement("company");
for(Iterator<Person> personIter = cp17.employee.iterator();personIter.hasNext();){
Person nextEmployee = personIter.next();
Element employee = companyElement.addElement("employee");
employee.addAttribute("name",nextEmployee.name);
employee.addAttribute("phoneNumber",nextEmployee.phoneNumber);
}
Document document2 = DocumentHelper.createDocument();
Element compnies = document.addElement("companies");
//move cp17 to document2 as a child of companies.
//ERROR companies.add(cp17);
XMLWriter xmlWriter = new XMLWriter();
try{
xmlWriter.write(document2);
xmlWriter.close();
}
catch(Exception e){
e.printStackTrace();
}
}
}
我创建了两个文档对象,现在我想将一个元素及其子元素移动到另一个元素。我该怎么做。谁能告诉我,谢谢。^_^
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用标准 DOM 方法 Document.importNode 将一个文档中的内容导入到另一个文档中。 http://www.dom4j.org/dom4j-1.6.1/apidocs/org/dom4j/dom/DOMDocument.html#importNode%28org.w3c.dom.Node,%20boolean%29
(假设这一行:
应该读作:)
Use the standard DOM method Document.importNode to bring content from one document into another. http://www.dom4j.org/dom4j-1.6.1/apidocs/org/dom4j/dom/DOMDocument.html#importNode%28org.w3c.dom.Node,%20boolean%29
(Assuming that this line:
is supposed to read:)
Element.createCopy()< /a> 可能是你应该使用的:
Element.createCopy() is probably what you should use: