Jason Tu
Jason Tu

Reputation: 59

Adding standalone=no field to XML declaration using XMLStreamWriter

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'?>

Upvotes: 1

Views: 2279

Answers (1)

Michael Kay
Michael Kay

Reputation: 163458

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();

Upvotes: 1

Related Questions