2015-02-09 61 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