Doh! Thanks Keith.
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);
...does the trick.
I'm having a little difficulty with the unmarshal though. Should it be
almost the same?....
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();
Unmarshaller unmarshaller = new Unmarshaller(handler);
unmarshaller.setValidation(false);
unmarshaller.setMapping(mapping);
Object obj = unmarshaller.unmarshal(inputSource);
...seems to ignore my mapping.
Jez
-----Original Message-----
From: Keith Visco [mailto:[EMAIL PROTECTED]
Sent: 28 September 2005 04:54
To: [email protected]
Subject: Re: [castor-user] easiest way to include arbitrary xml
Jez,
You can use solution 3 with a SAX ContentHandler instead of
DocumentHandler to get rid of the deprecation warnings. Should be as
easier as changing the XMLSerializer#asDocumentHandler call to
XMLSerializer#asContentHandler.
Another possibility is to either use a different SAX based serializer or
simply write one that doesn't do the escaping. Castor can marshal to any
SAX ContentHandler, so it really opens up the possibilities of how you
can marshal your objects.
--Keith
Jez Nicholson wrote:
> Hello Castor-List,
>
> This is a question that been asked a number of times before in various
> forms, but I still don't know _the_ definitive answer.
>
> Q: what is the simplest way to include a chunk of arbitrary xml when using
> Castor-XML unmarshalling so that it is mapped as a String.
>
> e.g. I am specifying a set of tests, one field contains a chunk of xml
input
> data and a second field contains the expected result. My users want to be
> able to edit the xml by hand in an xml editor, so that they can see that
the
> xml is valid.
>
> A(1): output-escape it using < > etc. This is the default behaviour.
>
> A(2): After the user has finished editing, enclose the arbitrary xml
chunks
> in CDATA blocks.
>
> A(3): A previous answer involved defining a handler, but I think that i
may
> have it wrong as Eclipse is telling me that org.xml.sax.DocumentHandler is
> deprecated:
>
> String[] cdata = {"html-tag","contents","question","text","feedback"};
> format.setCDataElements(cdata);
> format.setNonEscapingElements(cdata);
> XMLSerializer serializer = new XMLSerializer(writer,format);
> org.xml.sax.DocumentHandler handler = serializer.asDocumentHandler();
> Marshaller marsh = new Marshaller(handler);
>
> Regards,
> Jez
> --
> No virus found in this outgoing message.
> Checked by AVG Anti-Virus.
> Version: 7.0.344 / Virus Database: 267.11.6/111 - Release Date: 23/09/2005
>
>
> -------------------------------------------------
> If you wish to unsubscribe from this list, please
> send an empty message to the following address:
>
> [EMAIL PROTECTED]
> -------------------------------------------------
>
>
-------------------------------------------------
If you wish to unsubscribe from this list, please
send an empty message to the following address:
[EMAIL PROTECTED]
-------------------------------------------------
--
No virus found in this incoming message.
Checked by AVG Anti-Virus.
Version: 7.0.344 / Virus Database: 267.11.8/113 - Release Date: 27/09/2005
--
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.344 / Virus Database: 267.11.8/113 - Release Date: 27/09/2005
-------------------------------------------------
If you wish to unsubscribe from this list, please
send an empty message to the following address:
[EMAIL PROTECTED]
-------------------------------------------------