需要使用 dom4j 处理文档的帮助

发布于 2024-07-12 07:09:00 字数 1434 浏览 3 评论 0 原文

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();
        }
    }
}

我创建了两个文档对象,现在我想将一个元素及其子元素移动到另一个元素。我该怎么做。谁能告诉我,谢谢。^_^

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();
        }
    }
}

I creat two Document Object , now I want to move one Element and it's child Elements to another.How can i do that .Can anyone tell me, thank you.^_^

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千年*琉璃梦 2024-07-19 07:09:00

使用标准 DOM 方法 Document.importNode 将一个文档中的内容导入到另一个文档中。 http://www.dom4j.org/dom4j-1.6.1/apidocs/org/dom4j/dom/DOMDocument.html#importNode%28org.w3c.dom.Node,%20boolean%29

Element companyElement2= document2.importNode(companyElement, true);
companies.appendChild(companyElement2);

(假设这一行:

Element compnies = document.addElement("companies");

应该读作:)

Element companies = document2.addElement("companies");

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

Element companyElement2= document2.importNode(companyElement, true);
companies.appendChild(companyElement2);

(Assuming that this line:

Element compnies = document.addElement("companies");

is supposed to read:)

Element companies = document2.addElement("companies");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文