Adding standalone=no field to XML declaration using XMLStreamWriter

1.9k Views Asked by At

I'm currently using XMLStreamWriter to parse together an XML document. The only parameters that I'm allowed to pass in are "encoding" and "version", but I would like to have "standalone=no" in the declaration, as well. Here's what my output currently looks like:

<?xml version='1.0' encoding='UTF-8'?>

How can I make something like this?

<?xml version='1.0' encoding='UTF-8' standalone='no'?>
1

There are 1 best solutions below

0
On

XmlStreamWriter is an interface: it has more than one implementation!

If you install Saxon (any edition), you can create a Serializer using any of the serialization parameters defined in XSLT (for example standalone=yes), and then you can get an XmlStreamWriter that writes to this Serializer using Serializer.getXmlStreamWriter():

Processor p = new Processor(false);
Serializer s = p.newSerializer(System.out);
s.setOutputProperty(Property.STANDALONE, "no");
XmlStreamWriter writer = s.getXmlStreamWriter();