Solved

How do i put the contents of an XML dcoument into a SOAPBody?

Posted on 2004-11-01
600 Views
Last Modified: 2008-01-09
Hello,
I have a client application that talks to a web service.  The code works if i build of the body manually i.e.:

SOAPEnvelope env = ....
SOAPBody body = envelope.getBody().addBodyElement(envelope.createName("FirtsNode"));

What I really want to do is set the SOAPBody's payload to be the contents of an in memory XML document but I don't see a way to add a Document to the body.

Any ideas/solution?


Thanks in advance
Bruce
0
Question by:bqchristie
    14 Comments
     
    LVL 92

    Expert Comment

    by:objects
    try using the method:

     SOAPElement addChildElement(SOAPElement element)
              Add a SOAPElement as a child of this SOAPElement instance.
    0
     
    LVL 13

    Expert Comment

    by:petmagdy
    try this:

    <value>
    <![CDATA[ put ur xml here ]]>
    </value>
    0
     

    Author Comment

    by:bqchristie
    I appreciate the responses but I guess I didn't explain the problem well.  I have a Document object that I want to attach to the SOAPBody.  I don't see a constructor for a SOAPElement that takes a Document. Is there something I am missing there?  A sample would be much appreciated.

    Thanks





    0
     
    LVL 92

    Expert Comment

    by:objects
    you can wrap the Element from your tree that you want to include using a SOAPBodyElement.
    0
     

    Author Comment

    by:bqchristie
    how?
    0
     
    LVL 3

    Expert Comment

    by:vikraman_b
    Hi christie,
    Check this link ..

    http://www.javaworld.com/javaworld/jw-09-2003/jw-0912-webservices-p2.html

    Hope this may help u....
    0
     
    LVL 92

    Expert Comment

    by:objects
    something like:

    Element element = doc.getElementById(id);
    SoapBodyElement sbe = new SoapBodyElement(element);
    0
     
    LVL 3

    Expert Comment

    by:vikraman_b
    0
     

    Author Comment

    by:bqchristie
    thanks again for the suggestions...

    objects:
    your suggestion seems reasonable but SoapBodyElement can't be instatiated like that.

    vikraman b:
    those links seem to suggest that the XML doc has to go in as an attachment.  unfortunately the service expects the XML to be in the SOAPBody not as an attachment.
    0
     
    LVL 92

    Accepted Solution

    by:
    0
     

    Author Comment

    by:bqchristie
    k that compiles but I get an error when i run it.

          try {
                      SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();
                      SOAPConnection connection = scf.createConnection();
                      MessageFactory mf = MessageFactory.newInstance();
                      SOAPMessage msg = mf.createMessage();
                      SOAPPart part = (SOAPPart) msg.getSOAPPart();
                      SOAPEnvelope envelope = (SOAPEnvelope) part.getEnvelope();
                      SOAPHeader header = (SOAPHeader) envelope.getHeader();
                      SOAPBody body = (SOAPBody) envelope.getBody();
                      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                      factory.setValidating(false);
                Document doc = factory.newDocumentBuilder().parse(new File(XML_ROOT + "temp.xml"));
                //body.addChildElement(new SOAPBodyElement(envelope.createName("TKWhatver")));
                body.addChildElement(new SOAPBodyElement(doc.getDocumentElement()));
                connection.call(msg,endpoint);
                } catch (UnsupportedOperationException e) {
                      e.printStackTrace();
                } catch (SOAPException e) {
                      e.printStackTrace();
                      e.printStackTrace();
                } catch (IOException e) {
                      e.printStackTrace();
                } catch (ParserConfigurationException e) {
                      e.printStackTrace();
                } catch (SAXException e) {
                      e.printStackTrace();
                }


    javax.xml.soap.SOAPException: Cannot invoke Call with null namespace URI for method null
          at org.apache.axis.soap.SOAPConnectionImpl.call(SOAPConnectionImpl.java:110)
          at clientapp.BaseTest.doLogin(BaseTest.java:85)
          at clientapp.BaseTest.run(BaseTest.java:134)
          at java.lang.Thread.run(Thread.java:534)
    Caused by: Cannot invoke Call with null namespace URI for method null
          at org.apache.axis.client.Call.invoke(Call.java:2498)
          at org.apache.axis.client.Call.invoke(Call.java:1753)
          at org.apache.axis.soap.SOAPConnectionImpl.call(SOAPConnectionImpl.java:105)
          ... 3 more
    javax.xml.soap.SOAPException: Cannot invoke Call with null namespace URI for method null
          at org.apache.axis.soap.SOAPConnectionImpl.call(SOAPConnectionImpl.java:110)
          at clientapp.BaseTest.doLogin(BaseTest.java:85)
          at clientapp.BaseTest.run(BaseTest.java:134)
          at java.lang.Thread.run(Thread.java:534)
    Caused by: Cannot invoke Call with null namespace URI for method null
          at org.apache.axis.client.Call.invoke(Call.java:2498)
          at org.apache.axis.client.Call.invoke(Call.java:1753)
          at org.apache.axis.soap.SOAPConnectionImpl.call(SOAPConnectionImpl.java:105)
          ... 3 more
    0
     
    LVL 5

    Expert Comment

    by:Naeemg
    For better answer u've to submit ur code.
    0
     

    Author Comment

    by:bqchristie
    hi there,
    here is the source.  it dies in the doLogin method when it makes the call.  the service is a document/literal style.     The code works with the commented line:
                 //body.addChildElement(new SOAPBodyElement(envelope.createName("TKWhatver")));

    /*
     * Created on Oct 14, 2004
     *
     * TODO To change the template for this generated file go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    package clientapp;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.net.URL;
    import java.util.Iterator;

    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.soap.MessageFactory;
    import javax.xml.soap.Name;
    import javax.xml.soap.SOAPConnection;
    import javax.xml.soap.SOAPConnectionFactory;
    import javax.xml.soap.SOAPException;
    import javax.xml.soap.SOAPMessage;


    import org.apache.axis.SOAPPart;
    import org.apache.axis.message.MessageElement;
    import org.apache.axis.message.SOAPBody;
    import org.apache.axis.message.SOAPBodyElement;
    import org.apache.axis.message.SOAPEnvelope;
    import org.apache.axis.message.SOAPHeader;
    import org.exolab.castor.xml.MarshalException;
    import org.exolab.castor.xml.Marshaller;
    import org.exolab.castor.xml.ValidationException;
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.xml.sax.SAXException;

    import com.fidelity.instvoice.timekeeping.data.TimeKeepingRequest;


    public abstract class BaseTest {

          private static int QUERY_COUNT = 0;

          private static int LOGIN_COUNT = 0;

          private static int LOGOFF_COUNT = 0;

          private static int LTA_COUNT = 0;

          public String CLIENT_SESSION_ID = "";

          public static final String SESSION_NS = "http://core.webservices.workbrain.com/session";

          public static final String SESSION_LOCALPART = "sessionID";

          public static final String XML_ROOT = "C:\\eclipse\\workspace\\ws_fesc_client\\xml\\";

          public URL endpoint = null;

          public void doLogin(String filename) {
                System.out.println("doLogin()" + LOGIN_COUNT++);
                try {
                      SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();
                      SOAPConnection connection = scf.createConnection();
                      MessageFactory mf = MessageFactory.newInstance();
                      SOAPMessage msg = mf.createMessage();
                      SOAPPart part = (SOAPPart) msg.getSOAPPart();
                      SOAPEnvelope envelope = (SOAPEnvelope) part.getEnvelope();
                      SOAPHeader header = (SOAPHeader) envelope.getHeader();
                      SOAPBody body = (SOAPBody) envelope.getBody();
                      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                      factory.setValidating(false);
                Document doc = factory.newDocumentBuilder().parse(new File(XML_ROOT + "temp.xml"));
                //body.addChildElement(new SOAPBodyElement(envelope.createName("TKWhatver")));
                body.addChildElement(new SOAPBodyElement(doc.getDocumentElement()));
                connection.call(msg,endpoint);
                } catch (UnsupportedOperationException e) {
                      e.printStackTrace();
                } catch (SOAPException e) {
                      e.printStackTrace();
                      e.printStackTrace();
                } catch (IOException e) {
                      e.printStackTrace();
                } catch (ParserConfigurationException e) {
                      e.printStackTrace();
                } catch (SAXException e) {
                      e.printStackTrace();
                }
          }

          private SOAPMessage getMessage(TimeKeepingRequest request) {
                MessageFactory mf = null;
                SOAPMessage msg = null;
                try {
                      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                factory.setValidating(false);
                Document doc = factory.newDocumentBuilder().parse(new File(XML_ROOT + "temp.xml"));
                return null;
                      
                      //FileInputStream stream = new FileInputStream(file);
                      //msg = mf.createMessage(null, stream);
                      //msg.writeTo(System.out);
                } catch (FileNotFoundException e) {
                      e.printStackTrace();
                } catch (IOException e) {
                      e.printStackTrace();
                } catch (SAXException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                } catch (ParserConfigurationException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                }
    //            catch (MarshalException e) {
    //                  // TODO Auto-generated catch block
    //                  e.printStackTrace();
    //            } catch (ValidationException e) {
    //                  // TODO Auto-generated catch block
    //                  e.printStackTrace();
    //            }
                return msg;
          }

          public void run() {
                doLogin("login_req.xml");
          }

    }
    0
     

    Author Comment

    by:bqchristie
    Thanks for all the responses.     Objects' answer pretty much solved the problem.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Suggested Solutions

    1. Package the applet into a JAR file. The applet must be in a JAR file before a certificate can be attached to it. Use the jar JDK utility. If the applet was previously referenced with the help of a codebase attribute in  tag, replace the codebase …
    By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
    Video by: Michael
    Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
    The viewer will learn how to implement Singleton Design Pattern in Java.

    877 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now