在尝试构建XML文档Java时获取DOMException



我正在尝试使用Java构建以下XML文档:

<?xml version="1.0"?>
<doOrchestration>
    <request uuid="3f0cdf54-6846-433a-b23e-9a08fcd85634">
        <headers>
            <header name="Accept">application/xml</header>
        </headers>
        <method>GET</method>
        <path>SomePath here</path>
        <query>
        </query>
    </request>
</doOrchestration>

这里是我的代码:

public void buildXML() {
    try {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.newDocument();
        Element rootElement = doc.createElement("doOrchestration");
        rootElement.appendChild(rootElement);
        Element request = doc.createElement("request");
        rootElement.appendChild(rootElement);
        // TODO: make this UUID parameterizable
        Attr attrRequest = doc.createAttribute("uuid");
        attrRequest.setValue("3f0cdf54-6846-433a-b23e-9a08fcd85634");
        request.setAttributeNode(attrRequest);
        Attr attrHeader = doc.createAttribute("name");
        attrHeader.setValue("Accept");
        Element headers = doc.createElement("headers");
        rootElement.appendChild(headers);
        Element header = doc.createElement("header");
        headers.appendChild(header);
        header.appendChild(doc.createTextNode("application/xml"));
        Element method = doc.createElement("method");
        rootElement.appendChild(method);
        method.appendChild(doc.createTextNode("GET"));
        Element path = doc.createElement("path");
        rootElement.appendChild(path);
        path.appendChild(doc.createTextNode("Some request path here"));
        Element query = doc.createElement("query");
        rootElement.appendChild(query);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File("./generatedOrchestrationFile.xml"));
        transformer.transform(source, result);
        System.out.println("Success!");
    } 
    catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
}

当我运行这个程序时,我会遇到以下异常:

org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR: An attempt was made to insert a node where it is not permitted.

我在这里做错了什么?任何帮助或信息都将不胜感激,提前感谢。

更改代码以使用

        Element rootElement = doc.createElement("doOrchestration");
        doc.appendChild(rootElement);
        Element request = doc.createElement("request");
        rootElement.appendChild(request);

而不是

    Element rootElement = doc.createElement("doOrchestration");
    rootElement.appendChild(rootElement);
    Element request = doc.createElement("request");
    rootElement.appendChild(rootElement);

最新更新