Get elements of SOAP response

user1608396 picture user1608396 · Aug 26, 2012 · Viewed 7.6k times · Source

When I send a SOAP request using SAAJ API, I would receive a large SOAP response, in which I need to search for some nodes.

I am using following code but it just show the name of first node of body (HotelListResponse which has many children) and I have noticed that the loop just iterates once, (as shown in commented section).

In short: I need to know how to search for some nodes (preferably by their name) in a variable which its type is SOAPMessage. Thanks

SOAPMessage sm = response;   //this is the response

sm.writeTo(System.out);    //response is shown successfully in console

SOAPBody ReBody = sm.getSOAPBody();
Iterator sentIt = ReBody.getChildElements(); //create an iterator on elements
int counter=0;

while(sentIt.hasNext())
{
  SOAPBodyElement sentSBE = (SOAPBodyElement)sentIt.next();
  Iterator sentIt2 = sentSBE.getChildElements();
  SOAPElement sentSE = (SOAPElement)sentIt2.next();


   String sentID = sentSE.getNodeName();  //result is HotelListResponse
   counter++;  
  //System.out.println("sentID:"+sentID);
}
System.out.println("counter"+counter); //result is 1

Answer