import javax.xml.stream.XMLStreamWriter; import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamException; import java.io.StringWriter; XMLStreamWriter writer = null; StringWriter stringWriter = new StringWriter(); XMLOutputFactory factory = XMLOutputFactory.newInstance(); try { writer = factory.createXMLStreamWriter(stringWriter); // Write the default namespace writer.writeDefaultNamespace("http://example.com"); // Write the root element writer.writeStartElement("root"); writer.writeEndElement(); writer.flush(); } catch (XMLStreamException e) { e.printStackTrace(); } String xml = stringWriter.toString();This example creates an XMLStreamWriter and uses the writeDefaultNamespace() method to write the default namespace for the document. The root element is then written using the startElement() and endElement() methods. The resulting XML string is written to a StringWriter. The package library for javax.xml.stream is javax.xml.stream.