George Hernando
George Hernando

Reputation: 2650

Create Header Elements with Java Xerces XML Creation

How can I add standard header statements to XML documents that I generate using Java Xerces?

Something like this:?

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

I'm getting the <?xml> tag by default now, but how can I add the other header elements?

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

Upvotes: 0

Views: 239

Answers (1)

Cfx
Cfx

Reputation: 2312

The creation of the processing instruction is IMHO not well supported, but you can do the following:

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

Upvotes: 1

Related Questions