如何在生成XML时保留CDATA中的换行符?

cla*_*amp 6 java xml w3c newline cdata

我想写一些包含空格字符的文本,例如newlinetabxml文件,所以我使用

Element element = xmldoc.createElement("TestElement");
element.appendChild(xmldoc.createCDATASection(somestring));
Run Code Online (Sandbox Code Playgroud)

但是当我在使用中读回来的时候

Node vs =  xmldoc.getElementsByTagName("TestElement").item(0);
String x = vs.getFirstChild().getNodeValue();
Run Code Online (Sandbox Code Playgroud)

我得到一个没有新行的字符串了.
当我直接查看磁盘上的xml时,新行似乎得以保留.所以在读取xml文件时会出现问题.

我该如何保留换行符?

谢谢!

Avi*_*Dov 5

我不知道你如何解析和编写你的文档,但这是一个基于你的增强代码示例:

// creating the document in-memory                                                        
Document xmldoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();

Element element = xmldoc.createElement("TestElement");                                    
xmldoc.appendChild(element);                                                              
element.appendChild(xmldoc.createCDATASection("first line\nsecond line\n"));              

// serializing the xml to a string                                                        
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();             

DOMImplementationLS impl =                                                                
    (DOMImplementationLS)registry.getDOMImplementation("LS");                             

LSSerializer writer = impl.createLSSerializer();                                          
String str = writer.writeToString(xmldoc);                                                

// printing the xml for verification of whitespace in cdata                               
System.out.println("--- XML ---");                                                        
System.out.println(str);                                                                  

// de-serializing the xml from the string                                                 
final Charset charset = Charset.forName("utf-16");                                        
final ByteArrayInputStream input = new ByteArrayInputStream(str.getBytes(charset));       
Document xmldoc2 = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(input);

Node vs =  xmldoc2.getElementsByTagName("TestElement").item(0);                           
final Node child = vs.getFirstChild();                                                    
String x = child.getNodeValue();                                                          

// print the value, yay!                                                                  
System.out.println("--- Node Text ---");                                                  
System.out.println(x);                                                                    
Run Code Online (Sandbox Code Playgroud)

使用LSSerializer进行序列化是W3C的方法(参见此处).输出是预期的,带有行分隔符:

--- XML --- 
<?xml version="1.0" encoding="UTF-16"?>
<TestElement><![CDATA[first line
second line ]]></TestElement>
--- Node Text --- 
first line
second line
Run Code Online (Sandbox Code Playgroud)