[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 659
  • Last Modified:

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
0
bqchristie
Asked:
bqchristie
  • 6
  • 4
  • 2
  • +2
1 Solution
 
objectsCommented:
try using the method:

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

<value>
<![CDATA[ put ur xml here ]]>
</value>
0
 
bqchristieAuthor Commented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

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

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

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

Element element = doc.getElementById(id);
SoapBodyElement sbe = new SoapBodyElement(element);
0
 
vikraman_bCommented:
0
 
bqchristieAuthor Commented:
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
 
bqchristieAuthor Commented:
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
 
NaeemgCommented:
For better answer u've to submit ur code.
0
 
bqchristieAuthor Commented:
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
 
bqchristieAuthor Commented:
Thanks for all the responses.     Objects' answer pretty much solved the problem.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 6
  • 4
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now