JDOM:如何避免在漂亮打印的输出中出现
?

huangapple go评论80阅读模式
英文:

JDOM: How to avoid 
 in pretty printed output?

问题

def avoid_xml_output_issue():
    xml_text = """
    How do I avoid `
` in my XML output?

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

    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
    -------

    ```java
      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
        <?xml version="1.0" encoding="UTF-8"?>
        <el>line 1
        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][2] (search for `EndOfLineStringProperty`).

      [1]: http://www.jdom.org/docs/apidocs/org/jdom2/output/Format.html#setLineSeparator(java.lang.String)
      [2]: https://docs.oracle.com/javase/8/docs/api/javax/swing/text/DefaultEditorKit.html
    """
    
    return xml_text
英文:

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
    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

得分: 2

文档中引用的内容非常强调答案是“否”。

每当它在输入字符串中看到“\r”时,它都会将其替换为“
”。使用setLineSeparator只会改变“\n”被转换成什么。

如果调用:format.setLineSeparator(LineSeparator.NONE);,会发生什么情况?

英文:

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 "
". Using setLineSeparator just changes what "\n" gets converted to.

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

huangapple
  • 本文由 发表于 2020年7月22日 20:48:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63034592.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定