All_Safe
All_Safe

Reputation: 1399

Transformer CDATA_SECTION_ELEMENTS modify my XML content

I have this code:

Transformer transformer1 = TransformerFactory.newInstance().newTransformer();
final DOMResult domResult = new DOMResult();
marshaller.marshal(objectForMarshall, domResult);
transformer1.setOutputProperty(OutputKeys.INDENT, "yes");
transformer1.setOutputProperty(
        OutputKeys.CDATA_SECTION_ELEMENTS,
        "Parameters");
transformer1.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer1.transform(new DOMSource(domResult.getNode()), new StreamResult(new FileOutputStream("D:\\1.xml")));

In objectForMarshall has a field of the array type of bytes. The content of it I want to put in the CDATAsection.

I have this adapter:

public class CDATAAdapter extends XmlAdapter<String, byte[]> {

    @Override
    public String marshal(byte[] v) throws Exception {
        return new String(v);
    }

    @Override
    public byte[] unmarshal(String v) throws Exception {
        return v.getBytes();
    }

} 

After the transformation, in the final file I get the contents in CDATA, which is separated by a new line:

<ConditionInstance ActionConditionComponent="Regular" Id="1026">
      <Parameters><![CDATA[<regular_condition>

  <union type="AND">

    <operation allow_absent_property="false" type="BIGGER">

      <left>

        <Entity item="value" property="DateTo" type="datetime"/>

      </left>

      <right>

        <Entity item="value" property="DateTo" type="datetime"/>

      </right>

    </operation>

  </union>

</regular_condition>

]]></Parameters>
    </ConditionInstance>

Why is this happening, I can not understand? How can this be remedied? Why does the transformer change the content of my content?

If I delete:

transformer1.setOutputProperty(
            OutputKeys.CDATA_SECTION_ELEMENTS,
            cDataTagName);

New lines are not added!!!

<ConditionInstance ActionConditionComponent="Regular" Id="1026">
      <Parameters>&lt;regular_condition&gt;&#13;
  &lt;union type="AND"&gt;&#13;
    &lt;operation allow_absent_property="false" type="BIGGER"&gt;&#13;
      &lt;left&gt;&#13;
        &lt;Entity item="value" property="DateTo" type="datetime"/&gt;&#13;
      &lt;/left&gt;&#13;
      &lt;right&gt;&#13;
        &lt;Entity item="value" property="DateTo" type="datetime"/&gt;&#13;
      &lt;/right&gt;&#13;
    &lt;/operation&gt;&#13;
  &lt;/union&gt;&#13;
&lt;/regular_condition&gt;&#13;
</Parameters>
    </ConditionInstance>

Upvotes: 0

Views: 985

Answers (1)

Michael Kay
Michael Kay

Reputation: 163262

My guess is that the value of the Parameters element is a string containing CRLF (x13 x10) character pairs.

When you serialize this directly, the CR is serialized as the character reference &#13;, and the NL is serialized as an actual newline.

When you serialize it as part of a CDATA section, the CRLF is serialized as two characters, x13 followed by x10; and whatever software you are using to display the output is showing this as two newlines rather than one.

Upvotes: 1

Related Questions