SAAJ Newbie

Discussion in 'Java' started by writeameer@yahoo.com, Feb 20, 2007.

  1. Guest

    Sorry for the dumb question but I need help. The following is a copy
    of Request.java from JavaEETutorial.pdf form the java website.
    Except, instead of using an imaginary web site that didn't work
    (wombat.ztrade.com), I tried to use a real one (webserviceX.net). I
    get a java.util.NoSuchElementException.

    Help Please !



    import javax.xml.soap.*;
    import javax.xml.namespace.QName;

    import org.w3c.dom.NodeList;

    import java.util.Iterator;
    import java.net.URL;
    public class Request {
    public static void main(String[] args) {
    try {
    SOAPConnectionFactory soapConnectionFactory =
    SOAPConnectionFactory.newInstance();
    SOAPConnection connection =
    soapConnectionFactory.createConnection();
    MessageFactory factory = MessageFactory.newInstance();
    SOAPMessage message = factory.createMessage();

    SOAPHeader header = message.getSOAPHeader();
    SOAPBody body = message.getSOAPBody();
    header.detachNode();

    QName bodyName = new QName("http://www.webserviceX.NET/",
    "GetQuote","m");
    SOAPBodyElement bodyElement = body.addBodyElement(bodyName);

    QName name = new QName("symbol");
    SOAPElement symbol = bodyElement.addChildElement(name);
    symbol.addTextNode("SUNW");

    URL endpoint = new URL ("http://www.webservicex.net/
    stockquote.asmx");
    SOAPMessage response = connection.call(message, endpoint);
    connection.close();

    SOAPBody soapBody = response.getSOAPBody();

    Iterator iterator = soapBody.getChildElements(bodyName);
    bodyElement = (SOAPBodyElement)iterator.next();
    String lastPrice = bodyElement.getValue();

    System.out.print("The last price for SUNW is ");
    System.out.println(lastPrice);


    }
    catch (Exception ex) {
    ex.printStackTrace();
    }
    }
    }
    , Feb 20, 2007
    #1
    1. Advertising

  2. Guest

    1) Response.writeTo(System.out) will give a detailed error message.
    You need to set mime-type:
    message.getMimeHeaders().addHeader("SOAPAction", "http://
    www.webserviceX.NET/GetQuote");
    2) The service returns XML rather than price:
    Iterator iterator = soapBody.getChildElements(new QName("http://
    www.webserviceX.NET/", "GetQuoteResponse"));
    bodyElement = (SOAPBodyElement)iterator.next();
    String lastPrice = bodyElement.getTextContent();
    , Feb 20, 2007
    #2
    1. Advertising

  3. Guest

    Thank you for the help. I'm getting a SOAP response with "exception"
    for GetQuoteResult for the code listed below:
    How can I check I'm setting out the headers correctly in the request?


    <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soap:Body>
    <GetQuoteResponse xmlns="http://www.webserviceX.NET/">
    <GetQuoteResult>exception</GetQuoteResult>
    </GetQuoteResponse>
    </soap:Body>
    </soap:Envelope>


    import javax.xml.soap.*;
    import javax.xml.namespace.QName;

    import org.w3c.dom.NodeList;

    import java.util.Iterator;
    import java.net.URL;
    public class Request {
    public static void main(String[] args) {
    try {
    SOAPConnectionFactory soapConnectionFactory =
    SOAPConnectionFactory.newInstance();
    SOAPConnection connection =
    soapConnectionFactory.createConnection();
    MessageFactory factory = MessageFactory.newInstance();



    SOAPMessage message = factory.createMessage();
    SOAPHeader header = message.getSOAPHeader();
    SOAPBody body = message.getSOAPBody();
    header.detachNode();

    QName bodyName = new QName("http://www.webserviceX.NET/",
    "GetQuote","m");
    SOAPBodyElement bodyElement = body.addBodyElement(bodyName);

    QName name = new QName("symbol");
    SOAPElement symbol = bodyElement.addChildElement(name);
    symbol.addTextNode("SUNW");

    message.getMimeHeaders().addHeader("SOAPAction", "http://
    www.webserviceX.NET/GetQuote");
    URL endpoint = new URL ("http://www.webservicex.net/
    stockquote.asmx");
    SOAPMessage response = connection.call(message, endpoint);


    connection.close();

    SOAPBody soapBody = response.getSOAPBody();
    Iterator iterator = soapBody.getChildElements(new QName("http://
    www.webserviceX.NET/", "GetQuoteResponse"));
    bodyElement = (SOAPBodyElement)iterator.next();
    String lastPrice = bodyElement.getTextContent();

    System.out.println(lastPrice);

    response.writeTo(System.out);
    }
    catch (Exception ex) {
    ex.printStackTrace();
    }
    }
    }
    , Feb 21, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sunil

    to saaj or not to saaj

    Sunil, Jan 12, 2004, in forum: Java
    Replies:
    0
    Views:
    331
    Sunil
    Jan 12, 2004
  2. M. Turner
    Replies:
    0
    Views:
    599
    M. Turner
    Jun 25, 2004
  3. Jannar Molden

    SAAJ XML SOAP and element parameters

    Jannar Molden, Sep 2, 2004, in forum: Java
    Replies:
    1
    Views:
    986
    iksrazal
    Sep 3, 2004
  4. Son KwonNam

    SAAJ's DOM error

    Son KwonNam, Dec 15, 2004, in forum: Java
    Replies:
    2
    Views:
    5,161
    jddr70
    Apr 12, 2011
  5. frank
    Replies:
    0
    Views:
    396
    frank
    Apr 16, 2005
Loading...

Share This Page