我一直在尝试使用 XStreamMarshaller 在我的 Java Spring 项目中生成 XML 输出。我生成的 XML 元素文本中包含 CDATA 值。我在命令对象中手动创建此 CDATA 文本,如下所示:
f.setText("<![CDATA[cdata-text]]>");
XStreamMarshaller 生成的元素(下面的文本数据是别名)为:
<text-data><![CDATA[cdata-text]]></text-data>
上面的 XML 显示符合预期(请忽略上面元素名称中的反斜杠:<强>论坛格式)。但是,当我对生成的 XML 输出执行查看源代码时,我会看到以下元素: <![CDATA[cdata-text]]& gt;
。
问题:
如您所见,视图中的小于和大于字符已被替换为 <
和 >
来源。我需要我的客户端读取源代码并从 XML 输出中识别 CDATA 部分,而在上述场景中则不会。
有没有办法让 XStreamMarshaller 转义我提供的文本中的特殊字符?
我已将 Marshaller 的编码设置为 ISO-8859-1,但这也不起作用。如果 XStreamMarshaller 无法完成上述操作,您能否建议可以为我执行此操作的备用编组器/解组器?
// 按照 Paŭlo Ebermann 的建议显示我的 XML 并查看源代码:
XML 视图(如 IE 中显示):
An invalid character was found in text content. Error processing resource 'http://localhost:8080/file-service-framework/fil...
Los t
查看源代码:
<service id="file-text"><text-data><![CDATA[
Los túneles a través de las montañas hacen más fácil viajar por carretera.
]]></text-data></service>
非常感谢。
I have been trying to use XStreamMarshaller to generate XML output in my Java Spring project. The XML I am generating has CDATA values in the element text. I am manually creating this CDATA text in the command object like this:
f.setText("<![CDATA[cdata-text]]>");
The XStreamMarshaller generated the element(text-data below is an alias) as:
<text-data><![CDATA[cdata-text]]></text-data>
The above XML display is as expected (Please ignore the back slash in the above element name: forum formatting). But when I do a View Source on the XML output generated I see this for the element: <text-data><![CDATA[cdata-text]]></text-data>
.
Issue:
As you can see the less than and greater than characters have been replaced by <
and >
in the View Source. I need my client to read the source and identify CDATA section from the XML output which it will not in the above scenario.
Is there a way I can get the XStreamMarshaller to escape special characters in the text I provided?
I have set the encoding of the Marshaller to ISO-8859-1 but that does not work either. If the above cannot be done by XStreamMarshaller can you please suggest alternate marshallers/unmarshallers that can do this for me?
// Displaying my XML and View Source as suggested by Paŭlo Ebermann below:
XML View (as displayed in IE):
An invalid character was found in text content. Error processing resource 'http://localhost:8080/file-service-framework/fil...
Los t
View Source:
<service id="file-text"><text-data><![CDATA[
Los túneles a través de las montañas hacen más fácil viajar por carretera.
]]></text-data></service>
Thanks you very much.
发布评论
评论(1)
生成 CDATA 部分是 XML 生成库的任务,而不是其客户端的任务。因此,您只需编写即可
,然后库可以决定是否使用
...
]]>
或& lt;
-转义其内容。 对于接收者来说应该没有什么区别。编辑:
查看您的输出,它看起来是正确的(除了 CDATA) - 这里您必须处理您的输入,如上所述。
如果 IE 在这里抛出错误,很可能您没有声明正确的编码。
我对 Spring 框架了解不多,但是 Marshaller 使用的编码应该与 HTTP 标头 (
Content-Type: ... ;charset=... 中发送的编码相同。
) 或XML 序言(这两个也不应该不同)。
我建议使用
UTF-8
作为编码,因为它可以表示所有字符,而不仅仅是 Latin-1 字符。Generating CDATA sections is the task of your XML-generating library, not of its client. So you should simply have to write
and then the library can decide whether to use
<![CDATA[
...]]>
or the<
-escaping for its contents. It should make no difference for the receiver.Edit:
Looking at your output, it looks right (apart from the CDATA) - here you must work on your input, as said.
If IE throws an error here, most probably you don't have declared the right encoding.
I don't really know much about the Spring framework, but the encoding used by the Marshaller should be the same encoding as the encoding sent in either the HTTP header (
Content-Type: ... ;charset=...
) or the<?xml version="1.0" encoding="..." ?>
XML prologue (these two should not differ, too).I would recommend
UTF-8
as encoding everywhere, as this can represent all characters, not only the Latin-1 ones.