title: Parse Vertical Tab Encoding Character Present in XML
body: <p>I am dealing with a legacy system that produces an XML from input. This input sometimes contains <em>vertical tab</em> control characters. These characters then get encoded in the resulting XML. The vertical tab can be written in a Java string as <code>\u000B</code>. Here's an example code illustrating what happens (the original process takes the string from the input, and creates the text nodes without any preprocessing):</p>
  
  <pre><code>    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
      DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
      Document doc = documentBuilder.parse(new InputSource(new StringReader("&lt;xml&gt;&lt;/xml&gt;")));
      Element sample = doc.createElement("sample");
      sample.appendChild(doc.createTextNode("Hello\u000BWorld"));
      doc.getDocumentElement().appendChild(sample);
  
      TransformerFactory tf = TransformerFactory.newInstance();
      Transformer transformer = tf.newTransformer();
      transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
      transformer.setOutputProperty(OutputKeys.METHOD, "xml");
      transformer.setOutputProperty(OutputKeys.INDENT, "yes");
      transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
      transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
      transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(System.out, "UTF-8")));
  </code></pre>
  
  <p>This produces an XML document, which is invalid:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;xml&gt;
      &lt;sample&gt;Hello&amp;#11;World&lt;/sample&gt;
  &lt;/xml&gt;
  </code></pre>
  
  <p>Now I need to use the same parser, i.e.:</p>
  
  <pre><code>DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
  DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
  Document doc = documentBuilder.parse(new InputSource(new StringReader(theResultFromAbove)));
  </code></pre>
  
  <p>And produce the Document instance again. But this fails with:</p>
  
  <pre><code>[Fatal Error] :2:23: Character reference "&amp;#
  org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 23; Character reference "&amp;#
      at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:257)
  </code></pre>
  
  <p>I have control of the second parser, so I can search&amp;replace the <code>&amp;#11;</code> before it parses the output. I have also some control of the code that produces the XML in the first place, so I can simply remove the vertical tabs before all of this happens. </p>
  
  <p>However is there something I could do to <em>add</em> or <em>implement</em> the <code>&amp;#11;</code> entity so the parser would not only encode it, but also decode it properly?</p>