April 11, 2011

Support of CDATA section & Apex DOM Classes !

imageI saw this tweet today about apex-fast-xml-dom library. Unfortunately its true that FastXMLDom as of now can't support CDATA sections. Its because, FastXMLDom is wrapper on top of Spring'10 Apex DOM classes and DOM classes as of now doesn't support CDATA section.

In this post we will see

  • What is FastXMLDOM library for Apex ?
  • How to parse XML with CDATA sections in Apex ?
  • Promote an idea to have CDATA support in Apex DOM classes !

For those how don't know "What is FastXMLDom" ?

First, those who don't know what is FastXMLDom library, its basically wrapper on top of Spring'10 DOM classes. You must be thinking, what is the requirement of wrapping the API ?

I created this FastXMLDOM wrapper because

  • Apex DOM Classes are not following the W3C Dom model and require some learning curve to use. FastXmlDOM is based on W3C DOM API model, so no need to learn new API.
  • We all used awesome XMLDOM.cls library created by RonHess. This library did a great job of saving one from the hassle of using Apex XmlStream classes. But as its all written in Apex, it shares the "Script Statements" governor limits with your code. This doesn't hurts in general but for huge xml responses, it can be a trouble. (View benchmarks !)
  • FastXMLDom uses Spring'10 DOM classes internally, so all heavy duty operations of parsing the XML string and creating DOM out of it, is taken care of by native system library code. So this library tried to give what XmlDom.cls was giving, with lesser script statement consumption.

How can I parse CDATA in Apex ?

As Spring'10 DOM classes are not supporting CDATA element as of now, so both DOM classes  and FastXMLDom can't be used for that purpose.

Though, one can use XmlStream based classes i.e. XmlStreamReader to parse CDATA information from XML. Good news is that you can XMLDOM.cls library from Ron Hess, as this library uses XmlStreamReader internally so one can get CDATA information using it.  

Support Idea - We need CDATA section support Apex DOM Classes !

As XML is vital media of integrating external systems with SFDC, having support for CDATA section would be great. As of now one can't use DOM classes(+FastXMLDOM) to parse CDATA information, as mentioned about one can use XmlStreamReader or XmlDOM class for the same. But XmlStreamReader is hard to use and XmlDOM class can be heavy on script statements for huge XMLs.

So I was planning to add this as an idea on IdeaExchange, but luckily found one posted. I request you all to please vote up and support this idea.

Looking forward for your views and thoughts on this !