• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic


Vinod Kumar
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.
Shiva Jayaraman
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 =
SOAPConnection sc =
// Create an empty SOAP message
MessageFactory mf =
SOAPMessage message =
// 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 =
DocumentBuilder b =
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...
new DOMSource(
// 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 !!!
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!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic