A method to extract the content of the SOAP body. - Java javax.xml.soap

Java examples for javax.xml.soap:SOAPMessage

Description

A method to extract the content of the SOAP body.

Demo Code

/****************************************************************************
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL//* w ww  .jav  a  2  s . co  m*/

Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
the specific language governing rights and limitations under the License.

The Original Code is TEAM Engine.

The Initial Developer of the Original Code is Intecs SPA.  Portions created by
Intecs SPA are Copyright (C) 2008-2009, Intecs SPA. All Rights Reserved.

Contributor(s): No additional contributors to date
 ****************************************************************************/
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.xml.transform.OutputKeys;

public class Main{
    /**
     * A method to extract the content of the SOAP body.
     *
     * @param soapMessage
     *            the SOAP message.
     *
     * @return the content of the body of the input SOAP message.
     *
     * @author Simone Gianfranceschi
     */
    public static Document getSoapBody(Document soapMessage)
            throws Exception {
        Element envelope = soapMessage.getDocumentElement();
        Element body = DomUtils.getChildElement(DomUtils
                .getElementByTagName(envelope, envelope.getPrefix()
                        + ":Body"));
        Document content = DomUtils.createDocument(body);

        Element documentRoot = content.getDocumentElement();
        addNSdeclarations(envelope, documentRoot);
        addNSdeclarations(body, documentRoot);
        return content;
    }
    /**
     * A method to copy namespaces declarations.
     *
     * @param source
     *            the source message containing the namespaces to be copied on the target message.
     * @param target
     *            the target message.
     *
     * @author Simone Gianfranceschi
     */
    public static void addNSdeclarations(Element source, Element target)
            throws Exception {
        NamedNodeMap sourceAttributes = source.getAttributes();
        Attr attribute;
        String attributeName;
        for (int i = 0; i <= sourceAttributes.getLength() - 1; i++) {
            attribute = (Attr) sourceAttributes.item(i);
            attributeName = attribute.getName();
            if (attributeName.startsWith("xmlns")
                    && !attributeName.startsWith("xmlns:soap-env")) {
                //                System.out.println("XMLNS:" + attributeName+":"+attribute.getValue());
                target.setAttribute(attributeName, attribute.getValue());
            }
        }
    }
}

Related Tutorials