Perhaps something like this (apologies in advance for any inefficiency:
if(currentNode instanceof XMLNodeType.Text)
{
String toWrite = String.format("<![CDATA[%s]]>", currentNode.getText());
// or whatever retrieves text of the node
}
It looks like you need to massage the data to be valid XML. The process for this is of course highly dependent on your input. So essentially what occurs is you receive a big string that you need to convert into valid XML. The advantage here is that you can define a schema that the third party adheres to, this is a meeting with them so it is outside of the scope of discussion, but is worth mentioning. Once you have this schema defined you will know which nodes are considered "text" nodes and need to be wrapped in CDATA
blocks.
The basic idea is this:
List<String> textTags = new ArrayList<String>();
textTags.add("NODE");
//other things to add
String bigAwfulString = inputFromThirdParty();
String validXML = "";
for(String currentNode : bigAwfulString.split("yourRegexHere")
{
if(textTags.contains(currentNode)
{
validXML+=String.format("<![CDATA[%s]]>", currentNode.getText());
continue;
}
validXML+=currentNode;
}