Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

SAAJ  RSS feed

 
Vinod Kumar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,
I'm using the Soap w/ Attachments API to construct and send SOAP messages. Using SAAJ, I have to constuct individual elements and messages. But, If I have a SOAP request already constructed as a String object, is there way to wrap this String as a SOAPMessage and send it as it is?
Any help is greatly appreciated.
Thanks,
Vinu.
 
Shiva Jayaraman
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vinu,
Yes it is possible. The following code demonstrates how you can create a
SOAP message from the contents of a file.
--
Shiva Jayaraman
/**
* creating soap doc from contents of
* an xml file
*/
import javax.xml.soap.*;
import javax.xml.messaging.*;
import javax.xml.parsers.*;
import javax.xml.transform.dom.*;
import org.w3c.dom.*;
import java.net.*;
import java.io.*;
import java.util.*;
public class SOAPFromFile
{
public static void main(String [] args)
throws Exception
{
String url = "URL-Of-Your-Web-Service";
// Create a SOAP connection to the URL
SOAPConnectionFactory scf =
SOAPConnectionFactory.newInstance();
SOAPConnection sc =
scf.createConnection();
// Create an empty SOAP message
MessageFactory mf =
MessageFactory.newInstance();
SOAPMessage message =
mf.createMessage();
// Get its SOAP part
SOAPPart part = message.getSOAPPart();
// Parse the XML file.
// You can change this code segment to
// parse the xml from a string of
// whatever your source is
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder b =
dbf.newDocumentBuilder();
Document d =
b.parse(new File("a.txt"));

// set the content of soap part of
// empty doc from a DOMSource created
// using the document read in...
part.setContent(
new DOMSource(
d.getDocumentElement()));
// To look at your message,
// you can print it.
// message.writeTo(System.out);
// Send the message out to the URL
SOAPMessage response =
sc.call(message, new URLEndpoint(url));
// Print out the response !!!
response.writeTo(System.out);
}
}
 
Rich Raposa
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow! That wasy very helpful. I just spent an hour this morning searching the Internet for just such an example.
Thanks, Shiva!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!