Skip to main content

CDATA Update

4 replies [Last post]
malachid
Offline
Joined: 2004-05-16

I tried to use the CDATA example ( https://jaxb.dev.java.net/faq/JaxbCDATASample.java ), but it wasn't working...

Upon further inspection, I wrote this small test app to figure out why:

<br />
package com.intel.jaxb;</p>
<p>import org.apache.xml.serialize.OutputFormat;</p>
<p>public class OutputFormatter<br />
extends OutputFormat<br />
{<br />
	public OutputFormatter()<br />
	{<br />
		super();<br />
	}</p>
<p>	public boolean isCDataElement(String s)<br />
	{<br />
		System.out.format("isCDataElement(%s)=%b\r\n", s, new Boolean(super.isCDataElement(s)));<br />
		return super.isCDataElement(s);<br />
	}<br />
}<br />

It appears that the reason the default solution:

<br />
outputFormat.setCDataElements(new String[]{"config^title"});<br />

didn't work was because Xerces-J was not putting the namespace at the beginning (ie: just '^title'), probably due to default namespace. Removing the 'config' portion fixed it... so, working version:
<br />
outputFormat.setCDataElements(new String[]{"^title"});<br />

Hope that helps.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
ryan_shoemaker
Offline
Joined: 2003-06-11

I agree - I'll improve the comments in the sample to help avoid this confusion in the future. Other than that, it sounds like you're all set. Thanks for the feedback.

ryan_shoemaker
Offline
Joined: 2003-06-11

Malachi,

Can you tell me what sample you're trying to run? The link to the FAQ entry isn't 100% complete - it's just a template to show you how to setup the XMLSerializer and register it with the JAXB Marshaller. If there is a complete sample that should work and isn't, then please let us know and we'll take care of it.

Anyway, I tried the code on the po.xsd schema and po.xml document we ship with our samples and came to the same conclusion you did about the format of the namespaceURI^localname pairs. I used "^quantity" to marshal those elements as CDATA (since the schema doesn't define any namespaces) and it seemed to work fine.

malachid
Offline
Joined: 2004-05-16

> Can you tell me what sample you're trying to run?
> The link to the FAQ entry isn't 100% complete - it's
> s just a template to show you how to setup the
> XMLSerializer and register it with the JAXB
> Marshaller. If there is a complete sample that
> should work and isn't, then please let us know and
> we'll take care of it.

No, was just using the class from the FAQ to try to get it working. The sample that is posted, however, might be better served to have a comment and/or both {"test^title","^title"} so that people's initial attempt to implement it comes to better success.

> Anyway, I tried the code on the po.xsd schema and
> po.xml document we ship with our samples and came to
> the same conclusion you did about the format of the
> namespaceURI^localname pairs. I used "^quantity" to
> marshal those elements as CDATA (since the schema
> doesn't define any namespaces) and it seemed to work
> fine.

Yeah. I modified my helper class to add both types (in case it references one not in the default namespace). I can get it to serialize the CDATA correctly -- though I think I am going to have to try a different approach.

The problem that I am currently running into is that I need some of the content to be completely unaltered, as human-eyes will be opening the result in IE/notepad to look/edit it. Therefore, character-escaping doesn't work so well. Using the modified sample, I can get it to serialize the CDATA ok, but not in co-operation with indention. Either nothing is indented and there is CDATA in some places; or there is indention, but nothing is CDATA. The problem seems to be coming from the XMLSerializer have a global preserve whitespace... And tabbing/spacing is not added if you preserve. The correct solution would be to preserve whitespaces in CDATA and auto-format outside of it (since CDATA has to be completely unparsed).

I have spent the last few hours trying to figure out the best way to do this, and I think I am going to have to write my own Serializer. I think the correct solution would be to turn indent on, turn off preserve whitespace -- but then make it so that CDATA elements completely ignore both flags and always preserve. I don't see a simple way to do that with OutputFormat.

malachid
Offline
Joined: 2004-05-16

I think I got it.

I was doing:
[code]
setIndenting(true);
setIndent(4);
[/code]

the javadocs said that setIndenting(true) would reset it to an indent of 0, thus the reason I did it first.

I commented out the first line and it seems to be working fine now.