2015-11-04 17 views
0

我試圖建立使用Java下面的XML文檔時拋出:DOMException:獲取試圖建立一個XML文檔的Java

<?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.

我在這裏做錯了什麼?任何幫助或信息將不勝感激,謝謝所有事先。

+0

那麼哪一行代碼給出了異常? –

+0

我的錯誤發生在這裏:'rootElement.appendChild(rootElement);' – mosawi

+0

我想添加更多的細節,但stackoverflow抱怨我的問題主要是代碼。 – mosawi

回答

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

謝謝,你是一個主調試器! – mosawi