create SOAP Element From XML String

 import java.io.IOException;
import java.io.StringReader;
import java.util.Iterator;
import javax.xml.parsers.*;
import javax.xml.soap.*;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class Test{

public static SOAPElement createSOAPElementFromXMLString(String xmlString)
        throws ParserConfigurationException, IOException, SAXException
    {
        StringReader stringReader = new StringReader(xmlString);
        InputSource inputSource = new InputSource(stringReader);
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setNamespaceAware(true);
        SAXParser parser = factory.newSAXParser();
        SoapElementSaxHandler handler = new SoapElementSaxHandler();
        parser.parse(inputSource, handler);
        return handler.getSOAPElement();
    }

}


SoapElementSaxHandler.java
 ~~~~~~~~~~~~~~~~~~~~~~
import java.util.ArrayList;
import java.util.HashMap;
import javax.xml.soap.*;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SoapElementSaxHandler extends DefaultHandler
{

    public SoapElementSaxHandler()
    {
        prefixURIMapping = new HashMap();
        uris = new ArrayList();
        rootElement = null;
        currentElement = null;
    }

    public SOAPElement getSOAPElement()
    {
        return rootElement;
    }

    public void startDocument()
        throws SAXException
    {
        try
        {
            soapFactory = SOAPFactory.newInstance();
        }
        catch(SOAPException e)
        {
            throw new SAXException("Can't create a SOAPFactory instance", e);
        }
    }

    public void startPrefixMapping(String prefix, String uri)
    {
        prefixURIMapping.put(uri, prefix);
        uris.add(uri);
    }

    public void characters(char ch[], int start, int length)
        throws SAXException
    {
        String str = String.valueOf(ch);
        if(length > 0)
            try
            {
                currentElement.addTextNode(str.substring(start, start + length));
            }
            catch(SOAPException e)
            {
                throw new SAXException("Can't add a text node into SOAPElement from text", e);
            }
    }

    public void endElement(String uri, String localName, String qName)
    {
        if(currentElement != rootElement)
            currentElement = currentElement.getParentElement();
    }

    public void startElement(String namespaceURI, String localName, String qName, Attributes atts)
        throws SAXException
    {
        String prefix = (String)prefixURIMapping.get(namespaceURI);
        try
        {
            if(rootElement == null && currentElement == null)
            {
                rootElement = soapFactory.createElement(localName, prefix, namespaceURI);
                currentElement = rootElement;
            } else
            {
                currentElement = currentElement.addChildElement(localName, prefix, namespaceURI);
            }
            if(uris.size() > 0)
            {
                for(int i = 0; i < uris.size(); i++)
                {
                    String uri = (String)uris.get(i);
                    String pre = (String)prefixURIMapping.get(uri);
                    currentElement.addNamespaceDeclaration(pre, uri);
                }

                uris.clear();
            }
            for(int i = 0; i < atts.getLength(); i++)
            {
                javax.xml.soap.Name attriName;
                if(atts.getURI(i) != null)
                {
                    String attriPre = (String)prefixURIMapping.get(atts.getURI(i));
                    attriName = soapFactory.createName(atts.getLocalName(i), attriPre, atts.getURI(i));
                } else
                {
                    attriName = soapFactory.createName(atts.getLocalName(i));
                }
                currentElement.addAttribute(attriName, atts.getValue(i));
            }

        }
        catch(SOAPException e)
        {
            throw new SAXException(e);
        }
    }

    private HashMap prefixURIMapping;
    private ArrayList uris;
    private SOAPElement rootElement;
    private SOAPElement currentElement;
    private SOAPFactory soapFactory;
}


4 comments:

  1. Can you please explain the process flow after call happens to parser.parse(inputSource, handler);

    thanks!

    ReplyDelete
    Replies
    1. Hi Harry

      can you tell me where ur missing (not under standing).So that i can explain it to you

      Delete
  2. Thank you very much! :) It has been extremely useful :D

    ReplyDelete
  3. Hi, thank you very much!!!

    ReplyDelete