• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

SAAJ

 
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.
 
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);
}
}
 
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!
 
Seriously? That's what you're going with? I prefer this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!