使用CDATA_SECTION_ELEMENTS编组CDATA元素会添加回车符 [英] Marshalling CDATA elements with CDATA_SECTION_ELEMENTS adds carriage return characters
问题描述
我正在开发一个应用程序,该应用程序可将数据从数据库导出/导入数据库。数据提取的格式为XML,我正在使用JAXB进行序列化/(取消编组)。我希望将某些元素整理为CDATA元素,并正在使用此解决方案,它将 OutputKeys.CDATA_SECTION_ELEMENTS
设置为 Transformer
属性。
I'm working on an application that exports and imports data from / to a DB. The format of the data extract is XML and I'm using JAXB for the serialization / (un)marshalling. I want some elements to be marshalled as CDATA elements and am using this solution which sets OutputKeys.CDATA_SECTION_ELEMENTS
to the Transformer
properties.
到目前为止,它运行良好,但是现在我来到数据库中一个字段,该字段本身包含XML字符串,我也想将其放置在CDATA元素中。现在,由于某种原因, Transformer
现在在每行末尾添加了一些不必要的回车符( \r
) ,因此输出看起来像这样:
So far this was working quite well, but now I came to a field in the DB that itself contains an XML string, which I also would like to place inside of a CDATA element. Now, for some reason the Transformer
is now adding some unnecessary carriage return characters (\r
) to each line end, so that the output looks like this:
这是我的代码:
private static final String IDENT_LENGTH = "3";
private static final String CDATA_XML_ELEMENTS = "text definition note expression mandatoryExpression optionalExpression settingsXml";
public static void marshall(final Object rootObject, final Schema schema, final Writer writer) throws Exception {
final JAXBContext ctx = JAXBContext.newInstance(rootObject.getClass());
final Document document = createDocument();
final Marshaller marshaller = ctx.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setSchema(schema);
marshaller.marshal(rootObject, document);
createTransformer().transform(new DOMSource(document), new StreamResult(writer));
}
private static Document createDocument() throws ParserConfigurationException {
// the DocumentBuilderFactory is actually being hold in a singleton
final DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
return builderFactory.newDocumentBuilder().newDocument();
}
private static Transformer createTransformer() throws TransformerConfigurationException, TransformerFactoryConfigurationError {
// the TransformerFactory is actually being hold in a singleton
final TransformerFactory transformerFactory = TransformerFactory.newInstance();
final Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, CDATA_XML_ELEMENTS);
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", IDENT_LENGTH);
return transformer;
}
我正在传递 FileWriter
到 marshall
方法。
我带注释的模型类如下:
My annotated model class looks like this:
@XmlType
@XmlRootElement
public class DashboardSettings {
@XmlElement
private String settingsXml;
public String getSettingsXml() {
return settingsXml;
}
public void setSettingsXml(final String settingsXml) {
this.settingsXml = settingsXml;
}
}
注意:
来自数据库的XML字符串具有Windows样式的行尾,即 \r
和 \n
。我感觉JAXB期望当前使用Linux风格的输入(即仅 \n
),因此正在添加 \r
字符,因为我当前正在Windows计算机上运行。所以问题是,解决这个问题的最佳方法是什么?编组时是否可以传递任何参数来控制行尾字符?还是应该在编组之前将行尾转换为Linux样式?我的程序在不同平台(Windows,Linux,Mac OS)上的行为如何?
The XML string coming from the DB has Windows style line endings, i.e. \r
and \n
. I have the feeling that JAXB expects currently Linux style input (i. e. only \n
) and is therefore adding a \r
character because I'm currently running on a Windows machine. So the question is actually, what's the best way to solve this? Is there any parameter I can pass to control the line ending characters when marshalling? Or should I convert the line endings to Linux style prior marshalling? How will my program behave on different platforms (Windows, Linux, Mac OS)?
我不一定需要独立于平台的解决方案,如果输出在Windows,Linux或任何样式。我要避免的是组合 \r\r\n
,如上面的屏幕截图所示。
I don't necessarily need a platform independent solution, it's OK if the output is in Windows, Linux or whatever style. What I want to avoid is the combination \r\r\n
as shown in the above screenshot.
推荐答案
我意识到这个问题已经很老了,但是我遇到了类似的问题,所以也许答案可以对其他人有所帮助。
I realise this question is pretty old, but I ran into a similar problem, so maybe an answer can help someone else.
CDATA部分似乎是一个问题。就我而言,我使用的是 createCDATASection
方法来创建它们。如在您的示例中那样,当代码在Windows计算机上运行时,添加了附加的 CR
。
It seems to be an issue with CDATA sections. In my case, I was using the createCDATASection
method to create them. When the code was running on a Windows machine, an additional CR
was added, as in your example.
I
在我的项目中,XML文档随后被导出为字符串,然后以POST的形式发布到Linux服务器。因此,一旦生成字符串,我就删除了 CR
个字符,只剩下了 LF
:
In my project, the XML document was then exported to a string to POST to a Linux server. So once the string was generated, I just removed the CR
characters, leaving only the LF
:
myXmlString.replaceAll("\\r", "");
对于特定的问题,我可能不是一个合适的解决方案,但它可能再次为您提供帮助(
I might not be an appropriate solution for the specific question, but once again, it may help you (or someone else) find a solution.
注意:对于该特定项目,我坚持使用Java 7,因此它可能已修复。在较新的版本中。
Note: I'm stuck with Java 7 for this specific project, so it may have been fixed in a more recent version.
这篇关于使用CDATA_SECTION_ELEMENTS编组CDATA元素会添加回车符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!