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
Brian Cherry FWIBrian Cherry FWI 

Parsing XML DOM Node issue Null Pointer

So I'm parsing a Http repsonse and I keep getting a nullpointer exception when I know a child element is there. This is my first XML rodeo so I was hoping for suggestions.

My Debug log before adding the getchildelement is this:
 
15:22:10:905 USER_DEBUG [27]|DEBUG|nodeXMLNode[ELEMENT,createCustomerProfileResponse,AnetApi/xml/v1/schema/AnetApiSchema.xsd,null,[common.apex.api.dom.XmlNode$NamespaceDef@21526738, common.apex.api.dom.XmlNode$NamespaceDef@582ef6d5, common.apex.api.dom.XmlNode$NamespaceDef@61835476],[XMLNode[ELEMENT,messages,AnetApi/xml/v1/schema/AnetApiSchema.xsd,null,null,[XMLNode[ELEMENT,resultCode,AnetApi/xml/v1/schema/AnetApiSchema.xsd,null,null,[XMLNode[TEXT,null,null,null,null,null,Error,]],null,], XMLNode[ELEMENT,message,AnetApi/xml/v1/schema/AnetApiSchema.xsd,null,null,[XMLNode[ELEMENT,code,AnetApi/xml/v1/schema/AnetApiSchema.xsd,null,null,[XMLNode[TEXT,null,null,null,null,null,E00039,]],null,], XMLNode[ELEMENT,text,AnetApi/xml/v1/schema/AnetApiSchema.xsd,null,null,[XMLNode[TEXT,null,null,null,null,null,A duplicate record with ID 40545008 already exists.,]],null,]],null,]],null,], XMLNode[ELEMENT,customerPaymentProfileIdList,AnetApi/xml/v1/schema/AnetApiSchema.xsd,null,null,null,null,], XMLNode[ELEMENT,customerShippingAddressIdList,AnetApi/xml/v1/schema/AnetApiSchema.xsd,null,null,null,null,], XMLNode[ELEMENT,validationDirectResponseList,AnetApi/xml/v1/schema/AnetApiSchema.xsd,null,null,null,null,]],null,]
But when I add the getChildElement('resultCode',null).getText(); I'm getting a Null Pointer Exception even though the value is in the XML document. Any advice on what I'm missing?
 
public void process()
{
isSuccess = false;
string xml = getxmlRes();
Http h = new http();
HttpRequest req = new HttpRequest(); 
req.setEndpoint('https://apitest.authorize.net/xml/v1/request.api');
req.setMethod('POST');
req.setbody(xml);
HttpResponse res = h.send(req);
isSuccess = true;
Dom.Document doc = res.getBodyDocument();
status = res.getStatusCode();
Dom.XMLNode node = doc.getRootElement();
profilename = node.getChildElement('resultCode', null).gettext();
System.debug('ukpostcode: ' + node); 
body = res.getBody();
}

 
Daniel BallingerDaniel Ballinger
Are you able to share the WSDL for this service? I've made a tool that will generate the XML parser for you from the WSDL.
Brian Cherry FWIBrian Cherry FWI
https://api.authorize.net/soap/v1/Service.asmx?WSDL is the soap WSDL. I could change this to a soap request pretty easily.  Right now it's a XML request with a xsd.
Daniel BallingerDaniel Ballinger
OK, I ran this through the FuseIT SFDC Explorer Wsdl2Apex (http://www.fuseit.com/explorer) implementation for the CreateCustomProfile web method. It appeared to generate the required Apex fine.

I used the option to generate both the Wsdl2Apex equavlient code and the raw SOAP Http requests and parsing code.

Now the inner ServiceSoap class has both a CreateCustomerProfile and CreateCustomerProfile_Http method. The latter converts the XML response into a apiAuthorizeNetSoapV1.CreateCustomerProfileResponse_element.

I've posed the full code in https://gist.github.com/FishOfPrey/2e44344af0765a1eb9839df460bd0bb2