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

时间:2015-02-09 02:45:55

标签: java xml dtd xerces

如何将标准头语句添加到我使用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 个答案:

答案 0 :(得分: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);