Write Start Document
import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamWriter; public class MainClass { public static void main(String[] args) throws Exception { XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); XMLStreamWriter writer = outputFactory.createXMLStreamWriter(System.out); writer.writeStartDocument("1.0"); writer.writeStartElement("person"); writer.writeStartElement("name"); writer.writeStartElement("fname"); writer.writeCharacters("AAA"); writer.writeEndDocument(); writer.flush(); } }