Hi Mik,
 
I've wrestled with this one for an age. I refer you to an answer I got on the list yesterday to my email "easiest way to include arbitrary xml"....
 
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);

Would output your field contents non-escaped.

An alternative could be to create a simple FieldHandler

public class CDataFieldHandler extends AbstractFieldHandler
{
 
 /**
  * uses reflection to retrieve the value then wraps
  * it in a CDATA section
  */
 public Object getValue(Object object) throws IllegalStateException
 {
  FieldDescriptor f = getFieldDescriptor();
  String fieldName = f.getFieldName();

  Object value = PropertyUtils.getProperty(object,fieldName);
  if (value==null)
  {
   value = "";
  }
  return "<![CDATA[" + value.toString() + "]]>";
 }

}

Keith and co. please jump in if I am misguided.

Regards,

     Jez

 

-----Original Message-----
From: Michael Thome [mailto:[EMAIL PROTECTED]
Sent: 28 September 2005 16:25
To: [email protected]
Subject: [castor-user] Hinting XML Marshaller to use ![CDATA] ?

Is there a way to get the XML Marshaller to use a ![CDATA[...]] construct rather than standard escaping for specified fields/elements?  I realize that this is supposed to be an invisible choice, but the CDATA-escape is much more readable than &foo; when there are lots of escaped characters - in many cases, it is even considerably more compact.

Thanks,
    -mik

--
Michael Thome
BBN Technologies 10 Moulton St, Cambridge MA 02138 USA
phone: +1 617 873 1853
------------------------------------------------- If you wish to unsubscribe from this list, please send an empty message to the following address: [EMAIL PROTECTED] -------------------------------------------------

Reply via email to