1 Reply Latest reply: Mar 2, 2007 12:35 AM by jwenting RSS

    SOAP / SAAJ error, please help me.

    807606
      Hello.

      I've done a simple WebService wich add 2 numbers.
      When I use SAAJ, I obtain an error.

      Here il my SOAP code :
      import javax.xml.soap.*;
      import javax.xml.namespace.QName;
      import java.util.Iterator;
      import java.net.URL;
      
      public class Request {
        public static void main(String[] args)  {
          try {
               //Getting a SOAPConnection Object
            SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
            SOAPConnection connection = soapConnectionFactory.createConnection();
            
            // Creating a message
            MessageFactory factory = MessageFactory.newInstance();
            SOAPMessage message = factory.createMessage();
            
            SOAPHeader header = message.getSOAPHeader();
            
            // Add content to body
            SOAPBody body = message.getSOAPBody();
            QName bodyName = new QName("http://localhost","additionner","myWS");
            SOAPBodyElement bodyElement = body.addBodyElement(bodyName);
            
            // Delete "header"
            header.detachNode();
            
            // Adding content
            QName childname1 = new QName("http://localhost","valeur1","myWS");
            bodyElement.addChildElement(childname1).addTextNode("5");         
            
            QName childname2 = new QName("http://localhost","valeur2","myWS");
            bodyElement.addChildElement(childname2).addTextNode("6");        
            
            message.writeTo(System.out);
            System.out.println("\n");
            
            // call method who takes two arguments :
            //     message : message being sent
            //     endpoint : destination to which the message should go
            URL endpoint = new URL ("http://localhost:8080/testWS/services/CalculerWS");
            SOAPMessage response = connection.call(message, endpoint);
            
            // Close connection
            connection.close();
            
            //Getting the content of the message
            SOAPBody soapBody = response.getSOAPBody();
            Iterator iterator = soapBody.getChildElements(bodyName);
            bodyElement = (SOAPBodyElement)iterator.next();
            String add = bodyElement.getValue();
            
            // Display the returned value
            System.out.print("The return value of server: ");
            System.out.println(add);
      
          } catch (Exception ex) {
            ex.printStackTrace();
          }
        }
      } 
      And this is the error displays in the console :
      java.util.NoSuchElementException
           at com.sun.xml.messaging.saaj.soap.impl.ElementImpl$3.next(ElementImpl.java:723)
           at Request.main(Request.java:49)
      Have you got any idea to help me.

      Thanks a lot.