I have an external system I am making a SOAP HTTP POST request to that has a simple response. I need to store both returned values as string variables for further processing. Here is the response from the server I am trying to parse:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<AddDataResponse xmlns="https://generic-api.website.com">
<AddDataResult>
<RecordID>1234</RecordID>
<ErrorDescription>something went wrong</ErrorDescription>
</AddDataResult>
</AddDataResponse>
</soap:Body>
</soap:Envelope>
I have tried several patterns, but end up with the values being null. Here are the patterns I have tried:
//create DOM doc with HTTPResponse body
Dom.Document doc = res.getBodyDocument();
Dom.XMLNode root = doc.getRootElement();
string RecordId = root.getChildElement('RecordID', null).getText().trim();
string Error = getChildElement('ErrorDescription', null).getText().trim();
Here is a separate class pattern I tried , but the text values return null as well.
public class XMLparser{
Public void parseXML(String xmlfile)
{
DOM.Document xmlDOC = new DOM.Document();
xmlDOC.load(xmlfile);
DOM.XMLNode rootElement = xmlDOC.getRootElement();
System.Debug('$$$$ Root Element'+rootelement);
for(DOM.XMLNode xmlNodeObj:xmlDOC.getRootElement().getChildElements()){
System.Debug('$$$ Child Elements'+xmlNodeObj);
for(DOM.XMLNode xmlNodeObjChild:xmlNodeObj.getChildren())
{
System.Debug('$$$ Childrens'+xmlNodeObjChild.getChildren());
if(xmlNodeObjChild.getName()=='RecordId')
System.Debug(xmlNodeObjChild.getText().trim());
if(xmlNodeObjChild.getName()=='ErrorDescription')
System.Debug(xmlNodeObjChild.getText().trim());
}
}
}
}
Here is the debug log for the execution. It remains consistent. There seems to be 5 null values that come before the text value I am after.
[XMLNode[ELEMENT,AddDataResult,https://generic-api.website.com,null,null,
[XMLNode[ELEMENT,RecordID,https://generic-api.website.com,null,null,
[XMLNode[TEXT,null,null,null,null,null,1234,]],null,],
[XMLNode[ELEMENT,ErrorDescription,https://generic-api.website.com,null,null,
[XMLNode[TEXT,null,null,null,null,null,Client Inactive,]],null,]],null,]],null,]],null,]],null,]"|0xbcd6e9b
The values I am after are present in the text nodes, but I can't seem to get the value at the end of the delimited null values. Is there a better way to parse the response or to move past the null values? Any input is appreciate. Thanks!