XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(System.out); writer.writeStartDocument(); writer.writeStartElement("root"); writer.writeEndElement(); writer.writeEndDocument();
XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(System.out); writer.writeStartDocument("UTF-8", "1.0"); writer.writeStartElement("root"); writer.writeAttribute("attr1", "value1"); writer.writeAttribute("attr2", "value2"); writer.writeEndElement(); writer.writeEndDocument();This code creates an XMLStreamWriter that writes to the standard output and writes an XML declaration with the encoding UTF-8 and version number 1.0. It also writes a root element with two attributes. Overall, the javax.xml.stream package library is useful for developers who need to process large XML documents efficiently. The Streaming API for XML provides a low-level API that can be optimized for performance, but requires more knowledge and expertise compared to other APIs like DOM or SAX.