title: How to add DOCTYPE and xml processing instructions when marshalling with JAXB?
body: <p>I am marshalling (serializing) JAXB beans to output stream. How can I add DOCTYPE declaration and xml processing instructions to ouput? </p>
  
  <p>I am doing currently marshalling like this:</p>
  
  <pre><code>JAXBContext jaxbContext = JAXBContext.newInstance("com.example.package");
  Marshaller marshaller = jaxbContext.createMarshaller();
  marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
  
  SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
  Schema schema = schemaFactory.newSchema(schemaSource);
  marshaller.setSchema(schema);
  
  marshaller.marshal(object, output);
  </code></pre>
  
  <p>I'd like have output that looks something like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE Something SYSTEM "some.dtd"&gt;
  &lt;?xml-stylesheet type="text/xsl" href="some.xsl"?&gt;
  </code></pre>
  
  <p>JAXB bean are generated code so I don't want to change them.</p>
  
  <p>There are some hacks and undocumented tricks (see <a href="https://stackoverflow.com/questions/2153219/making-jaxb-generate-an-xml-processing-instruction">Making JAXB generate an XML processing instruction</a>)  to add the <a href="http://java.itags.org/java-enterprise/36260/" rel="nofollow noreferrer">xml processing instructions</a> and <a href="http://forums.java.net/jive/message.jspa?messageID=235226" rel="nofollow noreferrer">doctype</a>. But what is the preferred or right way to do this?</p>