Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-11-01
14
Medium Priority
?
640 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
Comment
Question by:bqchristie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
  • +2
14 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12468880
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
ID: 12468888
try this:

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

Author Comment

by:bqchristie
ID: 12469179
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

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

Author Comment

by:bqchristie
ID: 12469394
how?
0
 
LVL 3

Expert Comment

by:vikraman_b
ID: 12469443
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
ID: 12469480
something like:

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

Expert Comment

by:vikraman_b
ID: 12469553
0
 

Author Comment

by:bqchristie
ID: 12469620
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:
objects earned 1500 total points
ID: 12469658
0
 

Author Comment

by:bqchristie
ID: 12469922
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
ID: 12470092
For better answer u've to submit ur code.
0
 

Author Comment

by:bqchristie
ID: 12472083
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
ID: 12474241
Thanks for all the responses.     Objects' answer pretty much solved the problem.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

618 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