tags:

views:

56

answers:

1

I am using following code to format xml string. But it does not work if it has CDATA component inside, otehrwise it works fine. Any ideas?

OutputFormat format = new OutputFormat(document);
format.setLineWidth(65);
format.setIndenting(true);
format.setIndent(2);
Writer out = new StringWriter();
XMLSerializer serializer = new XMLSerializer(out, format);
serializer.serialize(document);
return out.toString();

EDIT : It does not format the xml below correctly :

<named-query name="aaaa">
  <query>
     <![CDATA[ 
        something
     ]]>
  </query>
</named-query>

It prints :

<named-query name="aaaa">
  <query><![CDATA[
asasasasasasasa
]]></query>
</named-query>
+2  A: 

The output looks correct to me:

<named-query name="aaaa"> 
  <query><![CDATA[ 
asasasasasasasa 
]]></query> 
</named-query>

If the following was generated:

<named-query name="aaaa"> 
    <query>
        <![CDATA[ 
            asasasasasasasa 
        ]]>
    </query> 
</named-query>

Then you have introduced withspace into the CDATA section, which is actually changing the value.

Blaise Doughan
Actually it is the other way!
fastcodejava