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
Maf_007Maf_007 

WS return string parsing error

Hi All, 

 

I am calling an webservice using wsdl class and getting a result successfully. The result is consists of seven strings and one of the strings contains the desired data in xml format. Now when I debug the string I can see the xml string but after parsing and fetching the required data, My debug shows null. 

 

Could anyone please help me on this??

 

Helper class to to Call WS via method in WSDL Class:

 

public class CallWS{
	List<XMLFile__c> xfiles = [select id, sendXML__c, sendXML2__c from XMLFile__c where name = :'XML FIles For WS'];    
    WS_Security_token__c stoken = [select Security_Token__c from WS_Security_token__c where Security_Token__c = :'xxxx'];
    
    String token = stoken.Security_Token__c;
    String xml1 = xfiles[0].sendXML__c;
    String xml2 = xfiles[0].sendXML2__c;
    String wsresult;
    
    //constructor method makes the web service call and gets the result back
    public void CallWS(){
    	NWebservice.NServiceSoap nws = new NWebservice.NServiceSoap();
        NWebservice.ProcessMultipleResponse_element result;
        
        result = nws.ProcessMultiple(token, xml1, xml2);//result consists of multiple xml strings
        String returnedxml = result.ProcessMultipleResult;// required string defined in wsdl
        wresult = returnedxml;
        String x = parsexml(wresult);
        
        system.debug(result);// shows all strings
        system.debug(returnedxml); // shows desired  string
        system.debug(x); // shows null....?? but there's value in this node 
    }
    
    private String parsexml(String toparse){
        String annsave;
        String costsave;
        String save2;
        String supersave;
        DOM.Document doc = new DOM.Document();
        
        try {
    		doc.load(toparse);
            	for(dom.XmlNode node : doc.getRootElement().getChildElements()){
                    if(node.getName()=='Annual-Saving'){
                    	annsave = node.getText().trim();
                        system.debug(annsave);
                    }
                    else if(node.getName()=='Cost-Saving'){
                    	costsave = node.getText().trim();
                        system.debug(costsave);
                    }
                    else if(node.getName()=='Extra-Save'){
                    	save2 = node.getText().trim();
                        system.debug(save2);
                    }
                    else if(node.getName()=='Total-Save'){
                    	supersave = node.getText().trim();
                        system.debug(supersave);
                    }
            	}
  			} catch (System.XMLException e) {  // invalid XML
    		e.getMessage();
  			}
        return annsave;
    }
}

  

Best Answer chosen by Admin (Salesforce Developers) 
Maf_007Maf_007

Hi All,

 

I have solved the problem. I was querying the wrong nodes. I was meant to query child of the child node and had to be done following way:

 

DOM.Document doc = new DOM.Document();

doc.load(toparse);
DOM.XMLNode node1 = toparse.getChildElement('Result', null);

for(node1 : doc.getRootElement().getChildElements()){
                    if(node.getName()=='Annual-Saving'){
                    	annsave = node.getText().trim();
                        system.debug(annsave);
                    }
                    else if(node.getName()=='Cost-Saving'){
                    	costsave = node.getText().trim();
                        system.debug(costsave);
                    }
                    else if(node.getName()=='Extra-Save'){
                    	save2 = node.getText().trim();
                        system.debug(save2);
                    }
                    else if(node.getName()=='Total-Save'){
                    	supersave = node.getText().trim();
                        system.debug(supersave);
                    }
            	}

 

Getting the desired strings in debug log....:)