function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
thoban2thoban2 

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 

thoban2thoban2

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....

 

18.0 APEX_CODE,DEBUG
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
myredpointmyredpoint

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:

 

 

<PhysicalLocation>
    <PhysicalAddress>
    <cityName>
        <FreeFormText><![CDATA[Celina]]></FreeFormText>
    </cityName>
    <GlobalCountryCode>US</GlobalCountryCode>
                    
    <NationalPostalCode>45822</NationalPostalCode>

    <postOfficeBoxIdentifier>
        <FreeFormText> </FreeFormText>
    </postOfficeBoxIdentifier>
    <regionName>
        <FreeFormText><![CDATA[OH]]></FreeFormText>
    </regionName>
</PhysicalAddress>
</PhysicalLocation>

 

Using the XmlNode Class Method getNode and getText I get following result:

 

cityName:
FreeFormText:
GlobalCountryCode: US
NationalPostalCode: 45822
postOfficeBoxIdentifier:
FreeFormText:
regionName:
FreeFormText:

 

If getText was working correctly.  I should get the CDATA for each node.  To test this, here is my code I'm using:

 

 

Dom.Xmlnode Level1 = doc.getRootElement();

for (Dom.Xmlnode l1 : Level1.getChildElements()) {
   a.Description += '\n' + '\r' + l1.getName() + ': ' + l1.getText();       
}

//I'm going down a few levels to get my output but the code is the same

 

Any input would be helpful.  Thanks.

 

 

robforcerobforce

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.

gitguygitguy

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.