使用 Java Xerces XML 创建创建 Header 元素

Create Header Elements with Java Xerces XML Creation

如何将标准 header 语句添加到使用 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> 标签,但我如何添加其他 header 元素?

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

恕我直言,处理指令的创建不太支持,但您可以执行以下操作:

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