向新根元素添加了文档

时间:2016-11-10 10:32:41

标签: java xml dom

我在下面的文档中我们要将该文档添加到标记下面。

当前文件

<Order>
</Order>

期待文件

<NewDoc>
    <Order>
    </Order>
</NewDoc>

我试过下面的代码(没有运气):

orderNewDoc.appendChild(orderOldDoc);

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

通过DOM完成的一种简单方法如下

/**
 * Created by RGOVIND on 11/10/2016.
 */
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
public class XMLRootAdd {
    public static void main(String[] args) throws Exception
    {
        String xml = "<Order></Order>";
        InputStream iStream =  new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8));

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();

        Document orderDoc = builder.parse(iStream);
        Node oldOrderNode = orderDoc.getDocumentElement();
        Document newDocument = builder.newDocument();
        Element newRoot = newDocument.createElement("NewDoc");
        newDocument.appendChild(newRoot);
        newRoot.appendChild(newDocument.importNode(oldOrderNode, true));

        /*Print and test*/
        DOMSource domSource = new DOMSource(newDocument);
        Writer writer = new OutputStreamWriter(System.out);
        StreamResult result = new StreamResult(writer);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.transform(domSource, result);
        writer.flush();

    }
}

打印:

<?xml version="1.0" encoding="UTF-8" standalone="no"?><NewDoc><Order/></NewDoc>