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().addBody Element(en velope.cre ateName("F irtsNode") );
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
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().addBody
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
try this:
<value>
<![CDATA[ put ur xml here ]]>
</value>
<value>
<![CDATA[ put ur xml here ]]>
</value>
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
Thanks
you can wrap the Element from your tree that you want to include using a SOAPBodyElement.
ASKER
how?
Hi christie,
Check this link ..
http://www.javaworld.com/javaworld/jw-09-2003/jw-0912-webservices-p2.html
Hope this may help u....
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);
Element element = doc.getElementById(id);
SoapBodyElement sbe = new SoapBodyElement(element);
ASKER
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
k that compiles but I get an error when i run it.
try {
SOAPConnectionFactory scf = SOAPConnectionFactory.newI nstance();
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.new Instance() ;
factory.setValidating(fals e);
Document doc = factory.newDocumentBuilder ().parse(n ew File(XML_ROOT + "temp.xml"));
//body.addChildElement(new SOAPBodyElement(envelope.c reateName( "TKWhatver ")));
body.addChildElement(new SOAPBodyElement(doc.getDoc umentEleme nt()));
connection.call(msg,endpoi nt);
} catch (UnsupportedOperationExcep tion e) {
e.printStackTrace();
} catch (SOAPException e) {
e.printStackTrace();
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationExcept ion e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
javax.xml.soap.SOAPExcepti on: Cannot invoke Call with null namespace URI for method null
at org.apache.axis.soap.SOAPC onnectionI mpl.call(S OAPConnect ionImpl.ja va:110)
at clientapp.BaseTest.doLogin (BaseTest. java:85)
at clientapp.BaseTest.run(Bas eTest.java :134)
at java.lang.Thread.run(Threa d.java:534 )
Caused by: Cannot invoke Call with null namespace URI for method null
at org.apache.axis.client.Cal l.invoke(C all.java:2 498)
at org.apache.axis.client.Cal l.invoke(C all.java:1 753)
at org.apache.axis.soap.SOAPC onnectionI mpl.call(S OAPConnect ionImpl.ja va:105)
... 3 more
javax.xml.soap.SOAPExcepti on: Cannot invoke Call with null namespace URI for method null
at org.apache.axis.soap.SOAPC onnectionI mpl.call(S OAPConnect ionImpl.ja va:110)
at clientapp.BaseTest.doLogin (BaseTest. java:85)
at clientapp.BaseTest.run(Bas eTest.java :134)
at java.lang.Thread.run(Threa d.java:534 )
Caused by: Cannot invoke Call with null namespace URI for method null
at org.apache.axis.client.Cal l.invoke(C all.java:2 498)
at org.apache.axis.client.Cal l.invoke(C all.java:1 753)
at org.apache.axis.soap.SOAPC onnectionI mpl.call(S OAPConnect ionImpl.ja va:105)
... 3 more
try {
SOAPConnectionFactory scf = SOAPConnectionFactory.newI
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.new
factory.setValidating(fals
Document doc = factory.newDocumentBuilder
//body.addChildElement(new
body.addChildElement(new SOAPBodyElement(doc.getDoc
connection.call(msg,endpoi
} catch (UnsupportedOperationExcep
e.printStackTrace();
} catch (SOAPException e) {
e.printStackTrace();
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationExcept
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
javax.xml.soap.SOAPExcepti
at org.apache.axis.soap.SOAPC
at clientapp.BaseTest.doLogin
at clientapp.BaseTest.run(Bas
at java.lang.Thread.run(Threa
Caused by: Cannot invoke Call with null namespace URI for method null
at org.apache.axis.client.Cal
at org.apache.axis.client.Cal
at org.apache.axis.soap.SOAPC
... 3 more
javax.xml.soap.SOAPExcepti
at org.apache.axis.soap.SOAPC
at clientapp.BaseTest.doLogin
at clientapp.BaseTest.run(Bas
at java.lang.Thread.run(Threa
Caused by: Cannot invoke Call with null namespace URI for method null
at org.apache.axis.client.Cal
at org.apache.axis.client.Cal
at org.apache.axis.soap.SOAPC
... 3 more
For better answer u've to submit ur code.
ASKER
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.c reateName( "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.FileNotFoundExcept ion;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.util.Iterator;
import javax.xml.parsers.Document BuilderFac tory;
import javax.xml.parsers.ParserCo nfiguratio nException ;
import javax.xml.soap.MessageFact ory;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPConnect ion;
import javax.xml.soap.SOAPConnect ionFactory ;
import javax.xml.soap.SOAPExcepti on;
import javax.xml.soap.SOAPMessage ;
import org.apache.axis.SOAPPart;
import org.apache.axis.message.Me ssageEleme nt;
import org.apache.axis.message.SO APBody;
import org.apache.axis.message.SO APBodyElem ent;
import org.apache.axis.message.SO APEnvelope ;
import org.apache.axis.message.SO APHeader;
import org.exolab.castor.xml.Mars halExcepti on;
import org.exolab.castor.xml.Mars haller;
import org.exolab.castor.xml.Vali dationExce ption;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
import com.fidelity.instvoice.tim ekeeping.d ata.TimeKe epingReque st;
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\\w s_fesc_cli ent\\xml\\ ";
public URL endpoint = null;
public void doLogin(String filename) {
System.out.println("doLogi n()" + LOGIN_COUNT++);
try {
SOAPConnectionFactory scf = SOAPConnectionFactory.newI nstance();
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.new Instance() ;
factory.setValidating(fals e);
Document doc = factory.newDocumentBuilder ().parse(n ew File(XML_ROOT + "temp.xml"));
//body.addChildElement(new SOAPBodyElement(envelope.c reateName( "TKWhatver ")));
body.addChildElement(new SOAPBodyElement(doc.getDoc umentEleme nt()));
connection.call(msg,endpoi nt);
} catch (UnsupportedOperationExcep tion e) {
e.printStackTrace();
} catch (SOAPException e) {
e.printStackTrace();
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationExcept ion e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
}
private SOAPMessage getMessage(TimeKeepingRequ est request) {
MessageFactory mf = null;
SOAPMessage msg = null;
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.new Instance() ;
factory.setValidating(fals e);
Document doc = factory.newDocumentBuilder ().parse(n ew 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 (ParserConfigurationExcept ion 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");
}
}
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
/*
* 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.FileNotFoundExcept
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.util.Iterator;
import javax.xml.parsers.Document
import javax.xml.parsers.ParserCo
import javax.xml.soap.MessageFact
import javax.xml.soap.Name;
import javax.xml.soap.SOAPConnect
import javax.xml.soap.SOAPConnect
import javax.xml.soap.SOAPExcepti
import javax.xml.soap.SOAPMessage
import org.apache.axis.SOAPPart;
import org.apache.axis.message.Me
import org.apache.axis.message.SO
import org.apache.axis.message.SO
import org.apache.axis.message.SO
import org.apache.axis.message.SO
import org.exolab.castor.xml.Mars
import org.exolab.castor.xml.Mars
import org.exolab.castor.xml.Vali
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
import com.fidelity.instvoice.tim
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\\w
public URL endpoint = null;
public void doLogin(String filename) {
System.out.println("doLogi
try {
SOAPConnectionFactory scf = SOAPConnectionFactory.newI
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.new
factory.setValidating(fals
Document doc = factory.newDocumentBuilder
//body.addChildElement(new
body.addChildElement(new SOAPBodyElement(doc.getDoc
connection.call(msg,endpoi
} catch (UnsupportedOperationExcep
e.printStackTrace();
} catch (SOAPException e) {
e.printStackTrace();
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationExcept
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
}
private SOAPMessage getMessage(TimeKeepingRequ
MessageFactory mf = null;
SOAPMessage msg = null;
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.new
factory.setValidating(fals
Document doc = factory.newDocumentBuilder
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 (ParserConfigurationExcept
// 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");
}
}
ASKER
Thanks for all the responses. Objects' answer pretty much solved the problem.
SOAPElement addChildElement(SOAPElemen
Add a SOAPElement as a child of this SOAPElement instance.