Search code examples
xmlfreemarkercdata

Handling CDATA escaped XML in Freemarker


I'm using Freemarker in my project to transform from one XML document to another. Due to crappy design or choice of canonical message format our supplier has chosen to embed some XML in CDATA escaped fields, probably as their chosen message standard does not handle all types of extensions. For whatever reason I now need to dig into this field and do some xpath queries.

Say ie:

<Invoice>
  ..
  <Note><![CDATA[<?xml version="1.0" encoding="utf-8"?><a><b>Value</b></a>]]></Note>
</Invoice>

Anyone has an idea how to get value "a/b/text()" in this kind of scenario?

I've thought about trying to clean the CDATA section manually and then parse it as XML, but my hopes are that Freemarker could do this for me.


Solution

  • FreeMarker doesn't parse XML, it just calls the usual API-s to do that, so FreeMarker can't help here. You will have to load the XML file into a String (char[] or whatever), remove those CDATA "tags", then parse the resulting String to DOM tree with javax.xml.parsers.DocumentBuilder, and pass that to FreeMarker. You will probably want to call NodeMode.simplify(theDomTree) before that however.