[SalesForce] converting xml response using Document class and xmlNode

I have to actually generate the following xml into a dom object, but failing to do so. This is my actual XML response :

 <Fault xmlns="http://schemas.microsoft.com/ws/2005/05/envelope/none">
<Code>
    <Value>Receiver</Value>
    <Subcode>
        <Value xmlns:a="http://schemas.microsoft.com/2009/WebFault">a:InternalServerError</Value>
    </Subcode>
</Code>
<Reason>
    <Text xml:lang="en-US">Internal Server Error</Text>
</Reason>
<Detail>
    <Error xmlns="http://api.endicia.com/labelservice/2013/02" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <Status>1001</Status>
        <Code>MissingOrInvalidElement</Code>
        <Message>Missing or invalid element: Country Of Origin, Customs item #1</Message>
    </Error>
</Detail>
</Fault>

This is my code to convert the above xml into dom object :

   Dom.Document xmlresponse = res.getBodyDocument();
   String msg = xmlresponse.getRootElement().getChildElement('Detail',null).getChildElement('Error',null).getChildElement('Message',null).getText();

But i'm having the attempt to de-reference a null object error. the problem arise actually when trying to get the "detail" node:

 Dom.Document doc = new Dom.Document();
 doc.load(s);// where s is my xml 
 dom.XmlNode xroot = doc.getRootElement();
 dom.XmlNode details = xroot.getChildElement('Detail', null); // this one generate the error. It can't find the "detail" node.

And i don't know why. when i debug the xroot objet, i get this :

XMLNode[ELEMENT,Fault,http://schemas.microsoft.com/ws/2005/05/envelope/none,null,[common.apex.api.dom.XmlNode$NamespaceDef@1c3efa27],[XMLNode[ELEMENT,Code,http://schemas.microsoft.com/ws/2005/05/envelope/none,null,null,[XMLNode[ELEMENT,Value,http://schemas.microsoft.com/ws/2005/05/envelope/none,null,null,[XMLNode[TEXT,null,null,null,null,null,Receiver,]],null,], XMLNode[ELEMENT,Subcode,http://schemas.microsoft.com/ws/2005/05/envelope/none,null,null,[XMLNode[ELEMENT,Value,http://schemas.microsoft.com/ws/2005/05/envelope/none,null,[common.apex.api.dom.XmlNode$NamespaceDef@715bae17],[XMLNode[TEXT,null,null,null,null,null,a:InternalServerError,]],null,]],null,]],null,], XMLNode[ELEMENT,Reason,http://schemas.microsoft.com/ws/2005/05/envelope/none,null,null,[XMLNode[ELEMENT,Text,http://schemas.microsoft.com/ws/2005/05/envelope/none,[common.apex.api.dom.XmlNode$Attribute@35851a92],[common.apex.api.dom.XmlNode$NamespaceDef@24ea845f],[XMLNode[TEXT,null,null,null,null,null,Internal Server Error,]],null,]],null,], XMLNode[ELEMENT,Detail,http://schemas.microsoft.com/ws/2005/05/envelope/none,null,null,[XMLNode[ELEMENT,Error,http://api.endicia.com/labelservice/2013/02,null,[common.apex.api.dom.XmlNode$NamespaceDef@4f6ead27, common.apex.api.dom.XmlNode$NamespaceDef@63e999cb],[XMLNode[ELEMENT,Status,http://api.endicia.com/labelservice/2013/02,null,null,[XMLNode[TEXT,null,null,null,null,null,1001,]],null,], XMLNode[ELEMENT,Code,http://api.endicia.com/labelservice/2013/02,null,null,[XMLNode[TEXT,null,null,null,null,null,MissingOrInvalidElement,]],null,], XMLNode[ELEMENT,Message,http://api.endicia.com/labelservice/2013/02,null,null,[XMLNode[TEXT,null,null,null,null,null,Missing or invalid element: Country Of Origin, Customs item #1,]],null,]],null,]],null,]],null,]

Best Answer

I can't figure out why, but this way of doing works for me:

Dom.Document doc = new Dom.Document();
doc.load(s);
dom.XmlNode xroot = doc.getRootElement();    
Dom.XmlNode[] rootElements = xroot.getChildren();
system.debug('## root size:' + rootElements.size());
system.debug('## element name:' + rootElements[2].getName());
dom.XmlNode details = rootElements[2];
Dom.XmlNode[] detailsElements = details.getChildren();
system.debug('## detail size:' + detailsElements.size());
system.debug('## element name:' + detailsElements[0].getName());
Dom.XmlNode message = detailsElements[0].getChildren()[2];
system.debug('## message:' + message.getText());
Related Topic