Core Java

How to Convert a String to SOAPMessage in Java

In enterprise Java applications, especially those integrating with legacy systems or third-party SOAP services, we may encounter raw SOAP XML strings that need to be processed programmatically. It is often necessary to convert these raw XML strings into objects to work effectively with such payloads. The SOAPMessage class, part of the javax.xml.soap package, provides an abstraction for working with SOAP messages. This article explores how to perform this conversion.

1. Maven Configuration

The following pom.xml includes the required dependencies to work with SOAP messages in Java using the Jakarta XML SOAP API. The first dependency, jakarta.xml.soap-api, provides the SOAP API interfaces, while the second, saaj-impl, supplies the implementation needed to create and manipulate SOAPMessage objects.

        <dependency>
            <groupId>jakarta.xml.soap</groupId>
            <artifactId>jakarta.xml.soap-api</artifactId>
            <version>3.0.2</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.messaging.saaj</groupId>
            <artifactId>saaj-impl</artifactId>
            <version>3.0.4</version>
        </dependency>

2. Java Code to Convert a String to SOAPMessage

The following Java program takes a raw SOAP XML string and converts it into a SOAPMessage object.

public class SoapStringConverter {

    public static void main(String[] args) {
    String soapXml = """
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                      xmlns:weat="http://www.webserviceX.NET">
       <soapenv:Header/>
       <soapenv:Body>
          <weat:GetWeather>
             <weat:CityName>Luton</weat:CityName>
             <weat:CountryName>United Kingdom</weat:CountryName>
          </weat:GetWeather>
       </soapenv:Body>
    </soapenv:Envelope>
    """;
        try {
            SOAPMessage soapMessage = convertStringToSOAPMessage(soapXml);
            soapMessage.writeTo(System.out);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static SOAPMessage convertStringToSOAPMessage(String xml) throws SOAPException, IOException {
        MessageFactory messageFactory = MessageFactory.newInstance();
        InputStream is = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8));
        return messageFactory.createMessage(null, is);
    }
}

This code defines a class SoapStringConverter that includes the method convertStringToSOAPMessage, which accepts a raw SOAP XML string and returns a SOAPMessage. It uses MessageFactory.newInstance() to create a new SOAP message and wraps the string in an InputStream, which is required by the factory method.

The conversion process begins by converting the raw XML string to a byte-based InputStream, as required by the MessageFactory.createMessage() method. This factory parses the input and builds a SOAPMessage object, which can then be used for further processing, such as reading headers, body elements, or even modifying the message.

Using soapMessage.writeTo(System.out) allows the entire SOAP message to be printed, confirming successful conversion of the string into a structured SOAP object. The inclusion of StandardCharsets.UTF_8 ensures proper character encoding during the conversion process.

The expected output is a well-formed SOAP envelope, printed as a single line of XML like this:

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                      xmlns:weat="http://www.webserviceX.NET">
       <soapenv:Header/>
       <soapenv:Body>
          <weat:GetWeather>
             <weat:CityName>Luton</weat:CityName>
             <weat:CountryName>United Kingdom</weat:CountryName>
          </weat:GetWeather>
       </soapenv:Body>
    </soapenv:Envelope>

3. Conclusion

In this article, we explored how to convert a raw SOAP XML string into a SOAPMessage object in Java using the Jakarta XML SOAP API and SAAJ implementation. A key component in this process is the MessageFactory class, which provides a way to create new SOAPMessage instances from various input sources, including XML streams. By leveraging MessageFactory, we can easily parse raw SOAP XML strings into structured SOAPMessage objects for further manipulation

4. Download the Source Code

This article covered how to convert a string into a SOAPMessage in Java.

Download
You can download the full source code of this example here: java convert string soapmessage

Omozegie Aziegbe

Omos Aziegbe is a technical writer and web/application developer with a BSc in Computer Science and Software Engineering from the University of Bedfordshire. Specializing in Java enterprise applications with the Jakarta EE framework, Omos also works with HTML5, CSS, and JavaScript for web development. As a freelance web developer, Omos combines technical expertise with research and writing on topics such as software engineering, programming, web application development, computer science, and technology.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button