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
RohiniHRohiniH 

Is it ok to send XML data fragments within CDATA,will the contents of CDATA be retrieved/parsed properly using XMLStreamReader

Hello,

Is it ok to send XML data fragments within CDATA.
Will the contents of CDATA be retrieved/parsed properly using XMLStreamReader?
I am getting error when retrieving the value of UID, TRANGTHAI for XML content as follows in the testcase only. It seems to be failing when getting UID
Strangely In the application the code seems to parse the xml obtained properly.

HttpResponse res = new HttpResponse();
        res.setHeader('Content-Type', 'text/xml; charset=utf-8');
        res.setBody('<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">' +'\n'+
                    '<soap:Body>' +'\n'+
                    '<GET_E1_Response xmlns="http://tempuri.org/">' +'\n'+
                    '<GET_E1_Result>'+
                    '<![CDATA[00#POD#Successful#<P_Id>S1109641</P_Id><UID>69744944</UID><NewDataSet>'+'\n'+
                    '<Table>'+'\n'+
                    '<TRANGTHAI>I</TRANGTHAI>'+'\n'+
                    '<NGAYNHAN>20131021</NGAYNHAN>'+'\n'+
                    '<GIONHAN>1000</GIONHAN>'+'\n'+
                    '</Table>'+'\n'+
                    '</NewDataSet>]]></GET_E1_Result>'+'\n'+
                 
                    '</GET_E1_Response>' +'\n'+
                    '</soap:Body>' +'\n'+
                    '</soap:Envelope>'
                    );


             
        res.setStatusCode(200);

///////////////////////////////////
XmlStreamReader reader = res.getXmlStreamReader();

String trangthai = ''; //status
        Boolean tFound = false;
              
        String UID = ''; 
        Boolean uFound = false;


        while(reader.hasNext()){
            reader.next();
          
            if (reader.getEventType() == XmlTag.START_ELEMENT && reader.getLocalName() == 'GET_E1_Result'){

                reader.Next();
                String txt = reader.getText();
                System.debug('one ' + txt );//pod
              
               if( !txt.startsWith('00')){

                   break;
                }
              
                if(txt.contains('00#POD#Picking up')){
                    trangthai ='Picking Up';
              
                    while (reader.hasNext()) {
                        reader.nextTag();
                          if (reader.getEventType() == XmlTag.START_ELEMENT && 'P_Id' == reader.getLocalName()) {
                                reader.next();
                                txt = reader.getText();
                                System.debug('PSPT ' + txt );//pod
                            }
                            if (reader.getEventType() == XmlTag.START_ELEMENT && 'UID' == reader.getLocalName()) {
                                reader.next();
                                txt = reader.getText();
                                UID = txt;
                                System.debug('UID ' + txt );//pod
                                break;
                            }
                      
                    }//while
                    break;
                }
              
              
                tFound = false;
                uFound = false;
              
                while(txt != '/NewDataSet'){
                    //System.debug('Loop value  '+a);//>

                    if(txt =='UID' ){
                        uFound = true;
                    }
                  
                    if(uFound == true){
                        if(reader.getText() == '/UID'){
                          
                            uFound = false;
                        }else{
                            System.debug('quackkk UID '+reader.getText());//>
                            if(reader.getText() != null && reader.getText() != '' && reader.getText() != '<'){
                                UID = reader.getText();
                                System.debug('UID IS ' + UID);//>
                            }
                        }
                    }




                    reader.Next();
                    txt = reader.getText();
                }//while
              
              
            }
        }

      System.debug('So the trangthai is '+trangthai);
      System.debug('So the UID is '+ UID);

But the code seems to work for following HTTP response where the CDATA does not have XML fragments


res.setBody('<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">' +
                    '<soap:Body>' +
                    '<GET_E1_Response xmlns="http://tempuri.org/">' +
                    '<GET_E1_Result>00#POD#Picking up#<P_Id>B2210417</P_Id><UID>70979231</UID></GET_E1_Result>'+
                 
                    '</GET_E1_Response>' +
                    '</soap:Body>' +
                    '</soap:Envelope>'
                    );

Kind regards,
Ruhi
@GM@GM


the problem is CDATA is vissible if you get string value of response directly using httpresponse.getbody() and then use XMLStreamReader