1

jdom で xml ドキュメントを解析すると、Document オブジェクトで一部の '\r' 文字が失われていることがわかりました。

例: aa\r\nbb

それを解析した後、要素 'b' のプロパティ 'text' が 'aa\nbb' であることがわかりました。

元の '\r' が失われた理由を知っている人はいますか? 任意の提案をいただければ幸いです。

ありがとう。

4

2 に答える 2

4

xml仕様では、行末がパーサーによって\nに正規化される必要があります。行末のセクションを参照してください。

于 2011-05-18T03:41:51.250 に答える
2

As @superfell points out, the XML specification requires an XML parser to normalize line endings to '\n' characters.

What can you do about it? Not a lot!

  1. You could use a character entity whose value is or contains a carriage return character. My reading of the normalization rules is that this will turn into a carriage return character in the normalized XML. However, this means you will have to change your input XML.

  2. You could change the application to replace the newlines with the appropriate platform-specific line endings ... after extracting them from the DOM.

  3. (You could even change the XML to represent the text in an encoded form; e.g. hexadecimal or base64. However, that's extremely ugly, and defeats the purpose of using XML.)

Of these, option 2 seems the least unattractive ...

于 2011-05-18T04:38:32.500 に答える