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

Parsing CDATA which has XML fragment within
Hello,
I am trying to parse XML content obtained via HTTP Response, It works fine when in application, but when the same XML string is parsed in testcase or say in developer console, then it fails as the CDATA's content which has XML fragment is not processed properly
Basically the CDATA has XML fragment within.
Following is the code...
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')){
//TODO UNCOMMENT required stmt
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 if the following response is send in the HTTP, it works fine with the above code, this response does not have CDATA,
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...
I am trying to parse XML content obtained via HTTP Response, It works fine when in application, but when the same XML string is parsed in testcase or say in developer console, then it fails as the CDATA's content which has XML fragment is not processed properly
Basically the CDATA has XML fragment within.
Following is the code...
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')){
//TODO UNCOMMENT required stmt
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 if the following response is send in the HTTP, it works fine with the above code, this response does not have CDATA,
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...