title: Java XML Numeric Character References
body: <p>I am having an issue when I parse an XML document that has numeric character references (ie &amp;#xA0).  The problem I am running into is that when the document is parsed, the &amp; is replaced with &amp;amp ; (without the space before the ;), so my parsed document will contain &amp;amp ;#xA0;.  How do I stop this from happening?  I have tried using <code>xmlDoc.setExpandEntityReferences(false)</code>, but that doesnt seem to change anything.</p>
  
  <p>Here is my code for parsing the document:</p>
  
  <pre><code>public static Document getXmlDoc(File xmlFile) throws ParserConfigurationException, SAXExeption, IOException {
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      factory.setIgnoringElementContentWhitespace(true);
      factory.setExpandEntityReferences(false);
      DocumentBuilder builder = factory.newDocumentBuilder();
      return builder.parse(xmlFile);
  }
  </code></pre>
  
  <p>Any help would be greatly appreciated.</p>
  
  <p>EDIT:</p>
  
  <p>The XML that is parsed form the above code is modified and then written back to a file.  The code to do this is below:</p>
  
  <pre><code>public static File saveXmlDoc(Document xmlDocument, String outputToDir, String outputFilename) throws IOException {
      String outputDir = outputToDir;
      if (!outputDir.endWith(File.separator)) outputDir += File.separator;
      if (!new FIle(outputDir).exists()) new File(outputDir).mkdir();
      File xmlFile = new File(outputDir + outputFilename);
      Transformer transformer = TransformerFactory.newInstance().newTransformer();
      transformer.setOutputProperty(OutputKeys.INDENT, "no");
      StreamResult saveResult = new StreamResult(outputDir + outputFilename);
      DOMSource source = new DOMSource(xmlDocument);
      transformer.transform(source, saveResult);
  
      return xmlFile;
  }
  </code></pre>
  
  <p>EDIT 2:</p>
  
  <p>Fixed a typo for <code>factory.setIgnoringElementContentWhitespace(true);</code>.</p>
  
  <p>EDIT 3 - My Solution:</p>
  
  <p>Since my reputation is too low to answer my own question, here is the solution I used to fix all of this.</p>
  
  <p>Here are the functions I changed in order to resolve this issue:</p>
  
  <p>To get the XML Document:</p>
  
  <pre><code>    public static Document getXmlDoc(File xmlFile) throws ParserConfigurationException, SAXException, IOException {
          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
          factory.setIgnoringElementContentWhitespace(true);
          factory.setExpandEntityReferences(false);
          DocumentBuilder builder = factory.newDocumentBuilder();
          return builder.parse(xmlFile);
      }
  </code></pre>
  
  <p>To save the XML Document:</p>
  
  <pre><code>    public static File saveXmlDoc(Document xmlDocument, String outputToDir, String outputFilename) throws Exception {
          readNodesForHexConversion(xmlDocument.getChildNodes());
          String xml = getXmlAsString(xmlDocument);
  
          // write the xml out to a file
          Exception writeError = null;
          File xmlFile = null;
          FileOutputStream fos = null;
          try {
              if (!new File(outputToDir).exists()) new File(outputToDir).mkdir();
              xmlFile = new File(outputToDir + outputFilename);
              if (!xmlFile.exists()) xmlFile.createNewFile();
              fos = new FileOutputStream(xmlFile);
  
              byte[] xmlBytes = xml.getBytes("UTF-8");
              fos.write(xmlBytes);
              fos.flush();
          } catch (Exception ex) {
              ex.printStackTrace();
              writeError = ex;
          } finally {
              if (fos != null) fos.close();
              if (writeError != null) throw writeError;
          }
  
          return xmlFile;
      }
  </code></pre>
  
  <p>To convert the XML Document to String:</p>
  
  <pre><code>        public static String getXmlAsString(Document xmlDocument) throws TransformerFactoryConfigurationError, TransformerException {
      DOMSource domSource = new DOMSource(xmlDocument);
      StringWriter writer = new StringWriter();
      StreamResult result = new StreamResult(writer);
      Transformer transformer;
      transformer = TransformerFactory.newInstance().newTransformer();
      transformer.transform(domSource, result);
      return writer.toString();
  }
  </code></pre>