Java tutorial
//package com.java2s; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; public class Main { /** * The default XML version to use in the XML document prefix. */ public static final String DEFAULT_XML_VERSION = "1.0"; /** * This method is used to write the XML document declaration at the beginning of an XML * document. This version is used when the standalone attribute is to be written as the * XMLStreamWriter provides no way for writing this attribute. * * @throws SAXException If an error occurs writing the identifier. */ public static void writeXMLDeclaration(XMLStreamWriter outputWriter, String xmlVersion, String encodingName, boolean standalone) throws XMLStreamException { if (xmlVersion == null || xmlVersion.trim().isEmpty()) { xmlVersion = DEFAULT_XML_VERSION; } StringBuilder xmlDeclarationBuilder = new StringBuilder(); xmlDeclarationBuilder.append("<?xml version=\""); xmlDeclarationBuilder.append(xmlVersion); xmlDeclarationBuilder.append('"'); if (encodingName != null) { xmlDeclarationBuilder.append(" encoding=\""); xmlDeclarationBuilder.append(encodingName); xmlDeclarationBuilder.append('"'); } xmlDeclarationBuilder.append(" standalone=\""); xmlDeclarationBuilder.append(standalone ? "yes" : "no"); xmlDeclarationBuilder.append("\"?>\n"); //Work around to avoid the angle brackets getting escaped. outputWriter.writeDTD(xmlDeclarationBuilder.toString()); } }