Link to home
Start Free TrialLog in
Avatar of bqchristie
bqchristie

asked on

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

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
Avatar of Mick Barry
Mick Barry
Flag of Australia image

try using the method:

 SOAPElement addChildElement(SOAPElement element)
          Add a SOAPElement as a child of this SOAPElement instance.
try this:

<value>
<![CDATA[ put ur xml here ]]>
</value>
Avatar of bqchristie
bqchristie

ASKER

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





you can wrap the Element from your tree that you want to include using a SOAPBodyElement.
how?
Hi christie,
Check this link ..

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

Hope this may help u....
something like:

Element element = doc.getElementById(id);
SoapBodyElement sbe = new SoapBodyElement(element);
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.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
For better answer u've to submit ur code.
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");
      }

}
Thanks for all the responses.     Objects' answer pretty much solved the problem.