You need to sign in to do that
Don't have an account?

DOM Support for ![CDATA[xxxxx]]
Does anyone know if the DOM Classes (Dom.Document and Dom.XmlNode) support the ![CDATA[]] construct.
It appears that if I include a node in an xml document with CDATA text, navigate to the node and then call getText() on it, it returns the raw entry with the full "!CDATA[.....]]". Even worse, if the CDATA block includes "<" or ">" Dom.XmlNode.load() throws a parse exception. Is there a way to make this work?
Thanks,
Tom
Below is some simple code that demonstrates the problem that I am having. In the third xml doc created, I have a cdata block that contains '<sftest2>'. The dom class throws a Parse Exception because <sftest2> is unmatched. However, most parsers ignore the data inside of the CDATA block.
Here is some code that demonstrates....
String docString = '<test>sftest</test>';
Dom.Document doc = new Dom.Document();
doc.load(docString);
Dom.xmlNode node = doc.getRootElement();
System.debug(' text=' + node.getText());
doc = new Dom.Document();
docString = '<test>![CDATA[sftest2]]</test>';
doc.load(docString);
node = doc.getRootElement();
System.debug(' text=' + node.getText());
doc = new Dom.Document();
docString = '<test>![CDATA[ <sftest2> ]]</test>';
doc.load(docString);
node = doc.getRootElement();
System.debug(' text=' + node.getText());
Output....
16:7:53.980|EXECUTION_STARTED
16:7:53.981|CODE_UNIT_STARTED|[EXTERNAL]System Log Window
16:7:53.986|METHOD_ENTRY|[4,1]|dom.Document.load(String)
16:7:53.986|METHOD_EXIT|[4,1]|load(String)
16:7:53.986|METHOD_ENTRY|[5,20]|dom.Document.getRootElement()
16:7:53.986|METHOD_EXIT|[5,20]|getRootElement()
16:7:53.986|METHOD_ENTRY|[6,1]|System.debug(String)
16:7:53.986|METHOD_ENTRY|[6,26]|dom.XmlNode.getText()
16:7:53.986|METHOD_EXIT|[6,26]|getText()
16:7:53.986|USER_DEBUG|[6,1]|DEBUG| text=sftest
16:7:53.986|METHOD_EXIT|[6,1]|debug(ANY)
16:7:53.986|METHOD_ENTRY|[10,1]|dom.Document.load(String)
16:7:53.987|METHOD_EXIT|[10,1]|load(String)
16:7:53.987|METHOD_ENTRY|[11,8]|dom.Document.getRootElement()
16:7:53.987|METHOD_EXIT|[11,8]|getRootElement()
16:7:53.987|METHOD_ENTRY|[12,1]|System.debug(String)
16:7:53.987|METHOD_ENTRY|[12,26]|dom.XmlNode.getText()
16:7:53.987|METHOD_EXIT|[12,26]|getText()
16:7:53.987|USER_DEBUG|[12,1]|DEBUG| text=![CDATA[sftest2]]
16:7:53.988|METHOD_EXIT|[12,1]|debug(ANY)
16:7:53.988|METHOD_ENTRY|[16,1]|dom.Document.load(String)
16:7:53.989|EXCEPTION_THROWN|[16,1]|System.XmlException: Failed to parse XML due to: end tag name </test> must match start tag name <sftest2> from line 1 (position: TEXT seen ...<sftest2> ]]</test>... @1:34)
16:7:53.989|METHOD_EXIT|[16,1]|load(String)
16:7:53.989|FATAL_ERROR|System.XmlException: Failed to parse XML due to: end tag name </test> must match start tag name <sftest2> from line 1 (position: TEXT seen ...<sftest2> ]]</test>... @1:34)
AnonymousBlock: line 16, column 1
16:7:53.989|CODE_UNIT_FINISHED
16:7:53.989|EXECUTION_FINISHED
I am having the same issue. I'll paste my code below. I think your error is because the CDATA is not in the correct structure from I've seen. Here is my XML stream:
Using the XmlNode Class Method getNode and getText I get following result:
If getText was working correctly. I should get the CDATA for each node. To test this, here is my code I'm using:
Any input would be helpful. Thanks.
I know this is an old post, but does anyone have a solution for this? The XML I'm trying to parse is too large to use the old XMLDom class that's floating around.
Nope. As this bug passes its anniversaries we still can't access CDATA sections. Attempts to system.debug() the nodes return
13:38:12.915 (2915018000)|USER_DEBUG|[26]|DEBUG|XMLNode[TEXT,null,null,null,null,null,
,]
13:38:12.915 (2915061000)|USER_DEBUG|[26]|DEBUG|XMLNode[TEXT,null,null,null,null,null,
,]
and trying getText() returns nothing, and the tags don't have names, and are basically useless. Too bad. I was really hoping to use static resources for test data.