英文:
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&#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][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 &#xD;
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 &#xD;
?
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("&#xD;", "")
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
得分: 2
文档中引用的内容非常强调答案是“否”。
每当它在输入字符串中看到“\r”时,它都会将其替换为“&#xD;”。使用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 "&#xD;". Using setLineSeparator just changes what "\n" gets converted to.
What happens if you call: format.setLineSeparator(LineSeparator.NONE);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论