XMLStreamWriter writer = factory.createXMLStreamWriter(output); writer.writeStartElement("book"); writer.writeStartElement("title"); writer.writeCharacters("The Catcher in the Rye"); writer.writeEndElement(); // closing title tag writer.writeEndElement(); // closing book tag
XMLStreamWriter writer = factory.createXMLStreamWriter(output); writer.writeStartElement("person"); writer.writeAttribute("id", "123"); writer.writeStartElement("name"); writer.writeCharacters("John Doe"); writer.writeEndElement(); // closing name tag writer.writeEndElement(); // closing person tagIn this code, an `XMLStreamWriter` object is created and writes a `person` element with an `id` attribute and a nested `name` element with its value. The `writeEndElement()` method is used to close the `name` and `person` elements. The `XMLStreamWriter` interface is part of the `javax.xml.stream` package library in Java.