title: Unable to unmarshall \u0000 after successfully marshalling it
body: <p>I have a <code>String</code> contating binary <code>0</code> inside in UTF-8 (<code>"A\u0000B"</code>). JAXB happily marshalls XML document containing such character but then fails to unmarshall it:</p>
  
  <pre><code>final JAXBContext jaxbContext = JAXBContext.newInstance(Root.class);
  final Marshaller marshaller = jaxbContext.createMarshaller();
  final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
  
  Root root = new Root();
  root.value = "A\u0000B";
  
  final ByteArrayOutputStream os = new ByteArrayOutputStream();
  marshaller.marshal(root, os);
  
  unmarshaller.unmarshal(new ByteArrayInputStream(os.toByteArray()));
  </code></pre>
  
  <p>The root class is just simple:</p>
  
  <pre><code>@XmlRootElement
  class Root { @XmlValue String value; }
  </code></pre>
  
  <p>Output XML contains binary <code>0</code> as well between <code>A</code> and <code>B</code> (in hex: <code>41 00 42</code>) which causes the following error during unmarshalling:</p>
  
  <pre><code>org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 63; 
  An invalid XML character (Unicode: 0x0) was found in the element content of the document.
  </code></pre>
  
  <p>Interestingly using raw DOM API (<a href="https://gist.github.com/3851796" rel="nofollow noreferrer">example</a>) produces escaped <code>0</code>: <code>A&amp;#0;B</code> but trying to read it back yields similar error. Also <code>0</code> (neither binary nor escaped) is not allowed by any XML parser or <code>xmllint</code> (see also: <a href="https://stackoverflow.com/questions/3038798">Python + Expat: Error on &amp;#0; entities</a>).</p>
  
  <h2>My questions:</h2>
  
  <ul>
  <li><p>why JAXB/DOM API allows creating <em>invalid</em> XML documents which it can not read back? Shouldn't it fail fast during marshalling?</p></li>
  <li><p>is there some elegant and global solution? I saw people tackling this problem by:</p>
  
  <ul>
  <li><p><a href="http://blog.mark-mclaren.info/2007/02/invalid-xml-characters-when-valid-utf8_5873.html" rel="nofollow noreferrer">manually ignoring special characters from input</a></p></li>
  <li><p><a href="http://blog.lesc.se/2009/03/escape-illegal-characters-with-jaxb-xml.html" rel="nofollow noreferrer">intercepting incoming stream</a> or even </p></li>
  <li><p><a href="https://stackoverflow.com/a/10312400/605744">implementing some internal <code>com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler</code> class</a></p></li>
  </ul></li>
  </ul>
  
  <p>But shouldn't mature XML stack in Java (I'm using 1.7.0_05) handle this either by default or by having some simple setting? I'm looking for escaping, ignoring or failing fast - but the default behavior of generating invalid XML is not acceptable. I believe such fundamental functionality should not require any extra coding on the client side.</p>