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,[[email protected]],[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,[[email protected]],[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,[[email protected]],[[email protected]],[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,[[email protected], [email protected]],[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,] 

Replay

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

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());

Category: apex Time: 2016-08-01 Views: 84

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.230 (s). 12 q(s)