How to convert a string to a SOAPMessage in Java?

前端 未结 6 908
轻奢々
轻奢々 2020-12-23 21:38

I wonder is there any way to convert a string to SOAPMessage?

Let me say I have a string as follows:

String send = "

        
6条回答
  •  春和景丽
    2020-12-23 22:04

    I just want to share my code snippet in Stack-Overflow, which might help some people.

    SOAP-XML String with Comments

    static boolean OMIT_XML_DECLARATION = true;
    static String soapXML = ""
                + " "
                + "  "
                + "API Gateway Oracle SOA Security and Management"
                + "  ";
    

    Convert SOAP-XML String to SOAP-Message Object

    public SOAPMessage getSoapMessage(String xmlData, boolean isToIgnoringComments) throws Exception {
        if (isToIgnoringComments) {
            Document doc = getDocument(xmlData); // SOAP MSG removing comment elements
            xmlData = toStringDocument(doc);
        }
        
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xmlData.getBytes());
        MimeHeaders mimeHeaders = new MimeHeaders();
        SOAPMessage message = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage(mimeHeaders, byteArrayInputStream);
        return message;
    }
    public static Document getDocument(String xmlData) throws Exception {
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        dbFactory.setNamespaceAware(true);
        dbFactory.setIgnoringComments(true);
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            InputSource ips = new org.xml.sax.InputSource(new StringReader(xmlData));
            Document doc = dBuilder.parse(ips);
        return doc;
    }
    

    Converting SOAP-Message Object to SOPA-XML String

    public static String toStringDocument(Document doc) throws TransformerException {
        StringWriter sw = new StringWriter();
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        if (OMIT_XML_DECLARATION) {
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        } else {
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        }
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    
        transformer.transform(new DOMSource(doc), new StreamResult(sw));
        return sw.toString(); // sw.getBuffer().toString();
    }
    

    Full Example:

    public class SOAPOperations {
    
        // dependency: groupId:xml-security, artifactId:xmlsec, version:1.3.0
        // dependency: groupId:xalan, artifactId:xalan, version:2.7.1
        public static void main(String[] args) throws Exception {
            
            SOAPOperations obj = new SOAPOperations();
            
            SOAPMessage soapMessage = obj.getSoapMessage(soapXML, true);
            System.out.println("SOAP Message Object:\n"+soapMessage);
            
            String soapMessageStr = obj.getSoapMessage(soapMessage);
            System.out.println("SOAP Message String:\n"+soapMessageStr);
            
            String soapBodyStr = obj.getSoapBody(soapMessage);
            System.out.println("SOAP Body String:\n"+soapBodyStr);
        }
    
        public String getSoapBody(SOAPMessage soapMessage) throws Exception {
            SOAPBody soapEnv = soapMessage.getSOAPBody();
            Document ownerDocument = soapEnv.extractContentAsDocument();
            String stringDocument = toStringDocument(ownerDocument);
            return stringDocument;
        }
        public String getSoapMessage(SOAPMessage soapMessage) throws Exception {
            SOAPEnvelope soapEnv = soapMessage.getSOAPPart().getEnvelope();
            Document ownerDocument = soapEnv.getOwnerDocument();
            String stringDocument = toStringDocument(ownerDocument);
            return stringDocument;
        }
    }
    

    @See SOAP links

    • Oracle - Creating a Simple Web Service and Client with JAX-WS
    • SOAP XML WS-Security signature verification

提交回复
热议问题