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

时间:2015-11-04 18:08:22

标签: java xml dom

我尝试使用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.

我在这里做错了什么?任何帮助或信息将非常感谢,谢谢所有提前。

1 个答案:

答案 0 :(得分:1)

更改要使用的代码

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