7

我想写一些包含空格字符的文本,例如newlinetab到一个 xml 文件中,所以我使用

Element element = xmldoc.createElement("TestElement");
element.appendChild(xmldoc.createCDATASection(somestring));

但是当我在使用中读回这个时

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

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

如何保留换行符?

谢谢!

4

5 回答 5

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

使用 LSSerializer 的序列化是 W3C 的实现方式(参见此处)。输出与预期的一样,带有行分隔符:

--- XML --- 
<?xml version="1.0" encoding="UTF-16"?>
<TestElement><![CDATA[first line
second line ]]></TestElement>
--- Node Text --- 
first line
second line
于 2009-08-08T11:43:03.853 回答
2

您需要使用 node.getNodeType() 检查每个节点的类型。如果类型是 CDATA_SECTION_NODE,则需要将 CDATA 保护连接到 node.getNodeValue。

于 2009-08-01T16:16:41.433 回答
2

您不一定必须使用 CDATA 来保留空白字符。XML规范 指定如何编码这些字符。

因此,例如,如果您的元素的 value 包含新空间,则应使用对其进行编码

  &#xA;

回车:

 &#xD;

等等

于 2009-08-01T16:48:56.953 回答
0

编辑:删掉所有不相关的东西

我很想知道您使用的是什么 DOM 实现,因为它没有反映我尝试过的几个 JVM 中的默认行为(它们带有 Xerces impl)。我也对您的文档有哪些换行符感兴趣。

我不确定 CDATA 是否应该保留空格是给定的。我怀疑这涉及到很多因素。DTD/模式不会影响空格的处理方式吗?

您可以尝试使用 xml:space="preserve" 属性。

于 2009-08-01T16:15:26.087 回答
0

xml:space='preserve' 不是。这仅适用于“所有空白”节点。也就是说,如果你想要空白节点

<this xml:space='preserve'> <has/>
<whitespace/>
</this>

但是请注意,那些空白节点只是空白。

我一直在努力让 Xerces 生成允许隔离 CDATA 内容的事件。我还没有解决办法。

于 2014-12-13T06:36:30.750 回答