Marshalling CDATA elements with CDATA_SECTION_ELEMENTS adds carriage return characters

断了今生、忘了曾经 提交于 2020-06-27 17:58:07

问题


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.

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: enter image description here

This is my code:

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

I'm passing a FileWriter to the marshall method.

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

NOTE:

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

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.


回答1:


I realise this question is pretty old, but I ran into a similar problem, so maybe an answer can help someone else.

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've tried a bunch of things to solve this "cleanly", to no avail.

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.

Note: I'm stuck with Java 7 for this specific project, so it may have been fixed in a more recent version.



来源:https://stackoverflow.com/questions/31483165/marshalling-cdata-elements-with-cdata-section-elements-adds-carriage-return-char

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!