title: Keep numeric character entity characters such as `&#10; &#13;` when parsing XML in Java
body: <p>I am parsing XML that contains numeric character entity characters such as (but not limited to) <code>&amp;#10; &amp;#13; &amp;lt; &amp;gt;</code> (line feed carriage return &lt; >) in Java. While parsing, I am appending text content of nodes to a StringBuffer to later write it out to a textfile.</p>
  
  <p>However, these unicode characters are resolved or transformed into newlines/whitespace when I write the String to a file or print it out. </p>
  
  <p>How can I keep the original numeric character entity characters symbols when iterating over nodes of an XML file in Java and storing the text content nodes to a String?</p>
  
  <p>Example of demo xml file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;ABCD version="2"&gt;    
      &lt;Field attributeWithChar="A string followed by special symbols &amp;#13;  &amp;#10;" /&gt;
  &lt;/ABCD&gt;
  </code></pre>
  
  <p>Example Java code. It loads the XML, iterates over the nodes and collects the text content of each node to a StringBuffer. After the iteration is over, it writes the StringBuffer to the console and also to a file (but no <code>&amp;#10; &amp;#13;</code>) symbols. </p>
  
  <p>What would be a way to keep these symbols when storing them to a String? Could you please help me? Thank you.</p>
  
  <pre><code>public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, TransformerException {   
      DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
      Document document = null;
      DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder();
      document = documentBuilder.parse(new File("path/to/demo.xml"));
      StringBuilder sb = new StringBuilder();
  
      NodeList nodeList = document.getElementsByTagName("*");
      for (int i = 0; i &lt; nodeList.getLength(); i++) {
          Node node = nodeList.item(i);
          if (node.getNodeType() == Node.ELEMENT_NODE) {
              NamedNodeMap nnp = node.getAttributes();
              for (int j = 0; j &lt; nnp.getLength(); j++) {
                  sb.append(nnp.item(j).getTextContent());
              }
          }
      }
      System.out.println(sb.toString());
  
      try (Writer writer = new BufferedWriter(new OutputStreamWriter(
              new FileOutputStream("path/to/demo_output.xml"), "UTF-8"))) {
          writer.write(sb.toString());
      }
  }
  </code></pre>