JDOM: How to avoid 
 in pretty printed output?

493 Views Asked by At

How do I avoid 
 in my XML output?

JDOM talks about this in the javadoc of the method Format.setLineSeparator.

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 \r, \n and \r\n, from a database, UI component, browser, program, etc.

Is it possible to configure JDOM to only write \r\n, and never 
?

Is this a specialty of JDOM or do other XML libraries behave the same? Will have to check...

The only idea I have so far is to iterate the complete document and remove any occurence of \r 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.

Another idea i have is to clean the output: xml.replace("
", "")

Example

  public static void main(String[] args) throws Exception
  {
    JTextPane pane = new JTextPane();
    pane.setText("line 1\r\nline 2");
    //    pane.setText("line 1\nline 2");
    Element element = new Element("el");
    element.setText(pane.getText());

    Format format = Format.getPrettyFormat();
    format.setLineSeparator("\r\n");
    StringWriter writer = new StringWriter();
    new XMLOutputter(format).output(new Document(element), writer);
    System.out.println(writer.toString());
  }

Output

    <?xml version="1.0" encoding="UTF-8"?>
    <el>line 1&#xD;
    line 2</el>

Context

I ran into this because JTextPane.getText() sometimes returned text with just \n, and sometimes with \r\n. JTextPane also has some pretty crazy end of line logic. See DefaultEditorKit (search for EndOfLineStringProperty).

1

There are 1 best solutions below

1
Not Saying On

The documentation you cite is pretty emphatic that the answer here is "no".

Anytime it sees a "\r" in the input string it will replace it with a "&#xD;". Using setLineSeparator just changes what "\n" gets converted to.

What happens if you call: format.setLineSeparator(LineSeparator.NONE);