1

I am trying to parse an XML with ![CDATA where multiple attributes are embedded into ![CDATA tags. By referring to multiple suggestions on parsing XML with CDATA, I have implemented below sample code to parse the xml and read the content in CDATA.

        String responseString =res.getBody();
        responseString = responseString.replaceAll( '<!\\[CDATA\\[', '' );
        responseString = responseString.replaceAll( ']]>', '' );
        DOM.Document doc=new DOM.Document();
        doc.load(responseString);
        DOM.XmlNode node=doc.getRootElement();

       if (node.getNodeType() == DOM.XMLNodeType.ELEMENT) {
            if(node.getName()=='IndexDetails'){
                 List<Dom.XMLNode>  childNodes  =node.getChildren();
                
                for(Dom.XMLNode childNode : childNodes ){
                    
                 System.debug('*** getName '+childNode.getName());
                 System.debug('*** getText '+childNode.getText());
                }
                
            }
        }



with above logic I am trying to find the values of XMM_DCN1 , XMM_WRDTE1 but when I use getName I am getting 'null' for all nodes and getText is giving a wrong values as below.

enter image description here

Any suggestions on what I am doing wrong?

XML reference

<IndexDetails><![CDATA[<DOCUMENT>
    <FIELD>
    <NAME>XMM_DCN1</NAME>
    <VALUE>sdfsdfsfd</VALUE>
    </FIELD>
    <FIELD>
    <NAME>XMM_WRDTE1</NAME>
    <VALUE></VALUE>
    </FIELD>
    <FIELD>
    <NAME>XMM_Ssdf</NAME>
    <VALUE>23/01/2017</VALUE>
    </FIELD>
    <FIELD>
    <NAME>XMM_DOCID1</NAME>
    <VALUE>657551</VALUE>
    </FIELD>
    <FIELD>
    <FILENAME>sdfsdf.pdf</FILENAME>
    </FIELD>
    <FIELD><PAGES>2</PAGES></FIELD>
    </DOCUMENT>]]></IndexDetails>

1 Answer 1

1

There's nothing wrong with your CDATA Removal, it's just that you needed to one level deeper before trying to get the names and values of the notes.

Each FIELD node actually has 2 more nodes - NAME and VALUE.

String responseString =res.getBody();
responseString = responseString.replaceAll( '<!\\[CDATA\\[', '' );
responseString = responseString.replaceAll( ']]>', '' );
DOM.Document doc=new DOM.Document();
doc.load(responseString);
DOM.XmlNode node=doc.getRootElement();

if (node.getNodeType() == DOM.XMLNodeType.ELEMENT) {
  if(node.getName()=='IndexDetails'){
    
    DOM.XmlNode documentNode = node.getChildElement('DOCUMENT', null);
    List<Dom.XMLNode>  childNodes  = documentNode.getChildren();
    
    //children of "DOCUMENT"
    for(Dom.XMLNode childNode : childNodes ){
      
      List<Dom.XMLNode>  childChildNodes  = childNode.getChildren();
      
      //children of "FIELD"
      for(Dom.XMLNode childChildNode : childChildNodes ){
        System.debug('*** getName '+ childChildNode.getName());
        System.debug('*** getText '+ childChildNode.getText());
      }
    }
      
  }
}

You were trying to get the name and value of the FIELD node - which it doesn't have. It has 2 more child nodes instead.

4
  • Hi Caspar, thanks for your inputs. I have tried updating the logic accordingly and see DOM.XmlNode documentNode = node.getChildElement('DOCUMENT', null); is returning null, only node.getChildren() is returning document node along with other children as below. (XMLNode[TEXT,null,null,null,null,null,<,], XMLNode[TEXT,null,null,null,null,null,DOCUMENT>. Do you see anything else I could do to crack this? Thanks again for the help.
    – Rajasekhar
    Commented Feb 14, 2022 at 3:38
  • I added the 5 lines you had in your code at the top - I'd left them off to make it more concise, but perhaps that was confusing. The above code I've actually run and can confirm that it works. Commented Feb 14, 2022 at 9:10
  • 1
    thanks much for the details, some how surprisingly I am still not able to crack this as while trying to get DOCUMENT node running into null pointer exception and node getChildren is giving return as (XMLNode[TEXT,null,null,null,null,null,<,], XMLNode[TEXT,null,null,null,null,null,DOCUMENT>
    – Rajasekhar
    Commented Mar 1, 2022 at 9:31
  • @Rajasekhar - I am facing a similar issue. You were able to fix the issue? Commented Apr 15, 2022 at 17:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .