If you use XMLStreamWriter , you can simply use writeNamespace() and writeAttribute() (or just writeAttribute() ).
XMLStreamWriter xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(System.out); xmlStreamWriter.writeStartDocument(); xmlStreamWriter.writeStartElement("YourRootElement"); xmlStreamWriter.writeNamespace("xsi", "http://www.w3.org/2000/10/XMLSchema-instance"); xmlStreamWriter.writeAttribute("http://www.w3.org/2000/10/XMLSchema-instance", "noNamespaceSchemaLocation", "path_to_your.xsd"); xmlStreamWriter.writeEndElement(); xmlStreamWriter.flush();
Output:
<?xml version="1.0" ?> <YourRootElement xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance" xsi:noNamespaceSchemaLocation="path_to_your.xsd"></YourRootElement>
For XMLEventWriter you can do this with add() ing a createAttribute() .
Regards, Max
Maximilian zellhofer
source share