Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java output XML file and CDATA

Tags:

java

xml

I am having a problem with the javax.xml.transform.Transformer class and its setOutputProperty method. I'm trying to get a XML output

   <name>
  <text>XXXXXXXXXX</text>
</name>
<questiontext format="html">
  <text><![CDATA[YYYYYYYYYYY]]></text>
</questiontext>

But using the:

        Transformer trans = transfac.newTransformer();
    trans.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "text");

causes for the both of text nodes to be embedded by CDATA tags like so:

    <name>
  <text><![CDATA[XXXXXXXXXX]]></text>
</name>
<questiontext format="html">
  <text><![CDATA[YYYYYYYYYYY]]></text>
</questiontext>

So i guess i need a way to specify the parent of the text element but i haven't found a way to do so and the javadocs don't specify which notation is used. Also i am not in a position to change the output XML format.

like image 406
maxPayne Avatar asked Mar 25 '26 20:03

maxPayne


1 Answers

You can't - the OutputKeys.CDATA_SECTION_ELEMENTS output property corresponds to the XSLT cdata-section-elements attribute of <xsl:output>, and that only allows you to define the elements in terms of QNames, not match expressions.

But it shouldn't matter since <foo>text</foo> and <foo><![CDATA[text]]></foo> are identical as far as an XML parser is concerned.

like image 157
Ian Roberts Avatar answered Mar 28 '26 10:03

Ian Roberts



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!