2015-02-09 45 views
0

如何將標準標題語句添加到使用Java Xerces生成的XML文檔中?使用Java Xerces XML創建創建標題元素

就像這樣:?

<?xml version="1.0" encoding="utf-8"?> 
<!--My Comment for this XML File --> 
<?TSMKey applanguage="EN" appversion="4.3.0" dtdversion="1.6.2"?> 
<!DOCTYPE KEYS SYSTEM "C:\TSM\System\DTD\TSMLte.dtd"> 

我現在越來越默認<?xml>標籤,但我怎麼能添加其他頭元素?

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); 
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); 

    Document doc = docBuilder.newDocument(); 
    Element rootElement = doc.createElement("TOP"); 
    doc.appendChild(rootElement); 

    DOMSource domSource = new DOMSource(doc); 

回答

1

處理指令的創建恕我直言,不能很好的支持,但你可以做到以下幾點:

Comment comment = document.createComment("My Comment for this XML File"); 
document.appendChild(comment); 
ProcessingInstruction processingInstruction = document.createProcessingInstruction("TSMKey", "applanguage=\"EN\" appversion=\"4.3.0\" dtdversion=\"1.6.2\""); 
document.appendChild(processingInstruction); 
Element rootElement = document.createElement("TOP"); 
document.appendChild(rootElement); 
+0

此註釋派我在正確的道路上,所以我標記爲正確的。我發現雖然我首先需要調用appendChild(rootElement),然後調用insertBefore(comment,rootElement)和insertBefore(processingInstruction,rootElement) – 2015-02-10 17:35:57