MessageFactory newInstance() Method



Description

The javax.xml.soap.MessageFactory.newInstance() method creates a new MessageFactory object that is an instance of the default implementation (SOAP 1.1), This method uses the following ordered lookup procedure to determine the MessageFactory implementation class to load: Use the javax.xml.soap.MessageFactory system property.

Declaration

Following is the declaration for javax.xml.soap.MessageFactory.newInstance() method

static MessageFactory newInstance() 

Return Value

a new MessageFactory object

Exception

SOAPException − if there was an error in creating the default implementation of the MessageFactory.

Example

The following example shows the usage of javax.xml.soap.MessageFactory.newInstance() method.

package com.tutorialspoint;

import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPMessage;

public class MessageFactoryDemo {
   public static void main(String[] args) {
      try {
         //create a default message factory
         MessageFactory messageFactory 
         = MessageFactory.newInstance();

         // create a new SOAPMessage
         SOAPMessage message = messageFactory.createMessage();
         
         //print the SOAPMessage on the console
         message.writeTo(System.out);

      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

If we compile the code and execute it, this will produce the following result −

<SOAP-ENV:Envelope 
xmlns:SOAP-ENV = "http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body/>
</SOAP-ENV:Envelope>
javax_xml_soap_messagefactory.htm
Advertisements