title: JDOM: How to avoid &#xD; in pretty printed output?
body: <p>How do I avoid <code>&amp;#xD;</code> in my XML output?</p>
  <p>JDOM talks about this in the javadoc of the method <a href="http://www.jdom.org/docs/apidocs/org/jdom2/output/Format.html#setLineSeparator(java.lang.String)" rel="nofollow noreferrer">Format.setLineSeparator</a>.</p>
  <p>For the sake of this question, please assume that I only get an XML document that I have to serialize. XML element texts might contain any combination of <code>\r</code>, <code>\n</code> and <code>\r\n</code>, from a database, UI component, browser, program, etc.</p>
  <p>Is it possible to configure JDOM to only write <code>\r\n</code>, and never <code>&amp;#xD;</code>?</p>
  <p>Is this a specialty of JDOM or do other XML libraries behave the same? Will have to check...</p>
  <p>The only idea I have so far is to iterate the complete document and remove any occurence of <code>\r</code> in any text property of any element. If that is indeed my only option, I would also be interested in a correct and readable way of doing so.</p>
  <p>Another idea i have is to clean the output: <code>xml.replace(&quot;&amp;#xD;&quot;, &quot;&quot;)</code></p>
  <h2>Example</h2>
  <pre><code>  public static void main(String[] args) throws Exception
    {
      JTextPane pane = new JTextPane();
      pane.setText(&quot;line 1\r\nline 2&quot;);
      //    pane.setText(&quot;line 1\nline 2&quot;);
      Element element = new Element(&quot;el&quot;);
      element.setText(pane.getText());
  
      Format format = Format.getPrettyFormat();
      format.setLineSeparator(&quot;\r\n&quot;);
      StringWriter writer = new StringWriter();
      new XMLOutputter(format).output(new Document(element), writer);
      System.out.println(writer.toString());
    }
  </code></pre>
  <h2>Output</h2>
  <pre><code>    &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
      &lt;el&gt;line 1&amp;#xD;
      line 2&lt;/el&gt;
  </code></pre>
  <h2>Context</h2>
  <p>I ran into this because JTextPane.getText() sometimes returned text with just <code>\n</code>, and sometimes with <code>\r\n</code>. JTextPane also has some pretty crazy end of line logic. See <a href="https://docs.oracle.com/javase/8/docs/api/javax/swing/text/DefaultEditorKit.html" rel="nofollow noreferrer">DefaultEditorKit</a> (search for <code>EndOfLineStringProperty</code>).</p>