I need to embed some HTML in some of my XML nodes and the consumer wants it inside a CDATA section. They want the CDATA to be preserved as <![CDATA[ ]]>.
But the opening and closing angle brackets are being escaped so they are receiving this instead <![CDATA[]]>
. I have tried a number of different methods but nothing seems to work. And there doesn't appear to be any functionality built into the Restlet framework. Has anyone else faced this issue and found a solution? Here's a sample of my code:
@Get
public Representation toXml() {
// Create a new SAX representation
SaxRepresentation result = new SaxRepresentation() {
public void write(org.restlet.ext.xml.XmlWriter writer)
throws IOException {
...
try {
// Start document
writer.startDocument();
// Append the root node
writer.startElement(COMPANIES);
// I want to start the CDATA section here.
writer.startElement(BRANCHES);
if((mfgRepList != null) && (mfgRepList.size() > 0)){
writer = MfgRep.getMfgRepHtml(writer, mfgRepList);
}
writer.endElement(BRANCHES);
// I want to end the CDATA section here.
writer.endElement(COMPANIES);
writer.endDocument();
} catch (SAXException e) {
throw new IOException(e.getMessage());
}
}; // end of write
}; // end of create new Sax representation
return result;
}
After inspecting the restlet code, it looks like they "escape" all strings/characters. So, in this case, I believe the only solution is to "write" around the xml writer through the output stream:
writer.getWriter().write("<![CDATA[");
// insert CDATA data here
writer.getWriter().write("]]");
There may be other solutions, but this seemed the most straight forward in this case. I believe it is an oversight in the restlet api.