> To answer the last question, yes, if you use a FieldHandler to wrap
> data in CDATA blocks, the serializer will escape the leading <
> character(s). The way to get around this has been discussed recently
> on the list, but basically consists of instantiating your own
> XMLSerializer and configuring it correctly.
That's what I thought and what I did. Unfortunately the post that
talked about this gave code that did not work for me. That code was...
> OutputFormat format = new OutputFormat(Method.XML, "UTF-8", true);
> String[] cdata = {"html-tag","contents","question","text","feedback"};
> format.setCDataElements(cdata);
> format.setNonEscapingElements(cdata);
> XMLSerializer serializer = new XMLSerializer(new
StringWriter(),format);
> ContentHandler handler = serializer.asContentHandler();
> Marshaller marsh = new Marshaller(handler);
> marsh.setValidation(false);
> marsh.setMapping(mapping);
> marsh.marshal(obj);
The problem was the construction of the cdata String[] element. For
some reason the isCDataElement() function in the OutputFormat object was
getting passed "^tag-name" instead of "tag-name". When I looked at the
code I see that the content before the carrot is the namespace for the
element. Since my XML does not have namespaces in it I had to construct
the string array with the leading carrot then it worked.
There are a few quirks to this...
* If you turn on CDATA for field "field1" then it is on in all instances
of field1, regardless of where in the XML it is. If you have two
objects with field1 on them and you want CDATA turned on for just one
you are out of luck. You could subclass the OutputFormat and
XmlSerializer objects to maintain an element name stack and then put
your own schema stack scanning logic in place so that you can have
context sensitive field name checking.
* The CDATA element will be rendered even if the data does not contain
escapable characters.
* If you use the pretty printing option your CDATA will be reformatted
anyway. You MUST use preserveWhitespace.
* When unmarshalling anything that contains CDATA you MUST use the
preserveWhitespace on the unmarshaller or your data will be reformatted
even though it is withing a CDATA tag.
Chris Elvart
-------------------------------------------------
If you wish to unsubscribe from this list, please
send an empty message to the following address:
[EMAIL PROTECTED]
-------------------------------------------------