Java XML Element Get getElement(SOAPMessage message, String tagname, String nsURI, int whichOne)

Here you can find the source of getElement(SOAPMessage message, String tagname, String nsURI, int whichOne)

Description

Get a SOAP Element from the SOAPMessage (SOAPbody inside).

License

Open Source License

Parameter

Parameter Description
message The SOAP message to be searched with.
tagname The tag name of element to be retrieved.
nsURI The namespace URI.
whichOne The nth child element to be returned.

Return

The element in the tagname specified.

Declaration

public static SOAPElement getElement(SOAPMessage message, String tagname, String nsURI, int whichOne)
        throws SOAPException 

Method Source Code

//package com.java2s;
/* /* ww  w  .  j  a v  a 2 s.  co  m*/
 * Copyright(c) 2005 Center for E-Commerce Infrastructure Development, The
 * University of Hong Kong (HKU). All Rights Reserved.
 *
 * This software is licensed under the GNU GENERAL PUBLIC LICENSE Version 2.0 [1]
 * 
 * [1] http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
 */

import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPException;

import javax.xml.soap.SOAPMessage;
import org.w3c.dom.NodeList;

public class Main {
    /**
     * Get a SOAP Element from the SOAPMessage (SOAPbody inside).
     * 
     * @param message   The SOAP message to be searched with.
     * @param tagname   The tag name of element to be retrieved.
     * @param nsURI      The namespace URI.
     * @param whichOne   The nth child element to be returned.
     * 
     * @return The element in the tagname specified.
     */
    public static SOAPElement getElement(SOAPMessage message, String tagname, String nsURI, int whichOne)
            throws SOAPException {
        if (message == null) // return null if the message is null.
            return null;
        // Get the soap body from the response.
        SOAPBody elementBody = message.getSOAPPart().getEnvelope().getBody();

        if (elementBody == null) // return null if the body is null.
            return null;

        // Find all the child with the childname.         
        NodeList nl = elementBody.getElementsByTagNameNS(nsURI, tagname);
        if (nl.getLength() <= whichOne) {
            return null;
        }
        return (SOAPElement) nl.item(whichOne);
    }
}

Related

  1. getElement(Element element, String tag)
  2. getElement(Element element, String tagName)
  3. getElement(Element element, String tagName, String namespace)
  4. getElement(Element root, String name)
  5. getElement(final Document doc, final String expression)
  6. getElementArray(Element config, String elementName)
  7. getElementAsBoolean(Element e, String name)
  8. getElementAsFloat(Element e, String name, Float dft)
  9. getElementAsInt(Element element)