title: Validation using JAXB and Stax to marshal XML document
body: <p>I have created an XML schema (foo.xsd) and used <code>xjc</code> to create my binding classes for JAXB.  Let's say the root element is <code>collection</code> and I am writing N <code>document</code> objects, which are complex types.</p>
  
  <p>Because I plan to write out large XML files, I am using Stax to write out the <code>collection</code> root element, and JAXB to marshal document subtrees using <code>Marshaller.marshal(JAXBElement, XMLEventWriter)</code>.  This is the approach recommended by <a href="http://jaxb.java.net/guide/Different_ways_of_marshalling.html#Marshalling_into_a_subtree" rel="nofollow noreferrer">jaxb's unofficial user's guide</a>.</p>
  
  <p>My question is, how can I validate the XML while it's being marshalled?  If I bind a schema to the JAXB marshaller (using <code>Marshaller.setSchema()</code>), I get validation errors because I am only marshalling a subtree (it's complaining that it's not seeing the <code>collection</code> root element").  I suppose what I really want to do is bind a schema to the <code>Stax XMLEventWriter</code> or something like that.</p>
  
  <p>Any comments on this overall approach would be helpful.  Basically I want to be able to use <code>JAXB</code> to marshal and unmarshal large XML documents without running out of memory, so if there's a better way to do this let me know.</p>