Hello,

I don't have a complete solution for you... but a hint how it should
work. I also just stumpled over the topic...

I read that the question of CDATA can / should / must be controlled by
the XML serializer.
Using Xerces you can create your own OutputFormat, set the element
names of those elements that should use CDATA into the OutputFormat,
create a XMLSerializer for the OutputFormat and use the
DocumentHandler of the XMLSerializer in the Marshaller...

A quick unit test showing how to:
package org.castor.xml;

import junit.framework.TestCase;

import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;

import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.XMLContext;

/**
 * To test how to serialize some element content as CDATA...
 *
 * @author Joachim Grueneis, jgrueneis_at_gmail_dot_com
 */
public class CdataTest extends TestCase {

    public CdataTest(String testName) {
        super(testName);
    }

    protected void setUp() throws Exception {
        super.setUp();
    }

    protected void tearDown() throws Exception {
        super.tearDown();
    }

    public void testCDataOutput() throws Exception {
        Foo foo = new Foo("Working & <h1>Testing CDATA</h1>");
        XMLContext xmlContext = new XMLContext();
        xmlContext.addClass(Foo.class);
        Marshaller m = xmlContext.createMarshaller();
        XMLSerializer serializer = getXMLSerializer();
        m.marshal(foo, serializer.asContentHandler());
    }

    private static XMLSerializer getXMLSerializer() {
        OutputFormat of = new OutputFormat();
        of.setCDataElements(new String[] { "^content" });
        of.setPreserveSpace(true);
        of.setIndenting(true);
        XMLSerializer serializer = new XMLSerializer(of);
        serializer.setOutputByteStream(System.out);
        return serializer;
    }

    /**
     * My domain class for testing
     */
    public class Foo {
        private String content;
        public Foo(String content) {
            this.content = content;
        }
        public String getContent() {
            return content;
        }
    }
}


2008/6/25 Agarwal, Rahul <[EMAIL PROTECTED]>:
> In my object I have a URL which contains "&" so I would like to marshall it
> out as <url><![CDATA[http://example.com?a=4&b=5]]></url>
>
>
>
> I have tried to search around but no satisfactory solutions. How can modify
> the mapping file to have it marshall it out as CDATA and not esacpe to
> "&amp;" which it does currently?
>
>
>
> Thanks

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply via email to