Example usage for javax.xml.soap SOAPBodyElement getChildElements

List of usage examples for javax.xml.soap SOAPBodyElement getChildElements

Introduction

In this page you can find the example usage for javax.xml.soap SOAPBodyElement getChildElements.

Prototype

public Iterator<Node> getChildElements();

Source Link

Document

Returns an Iterator over all the immediate child Node s of this element.

Usage

From source file:com.offbynull.portmapper.upnpigd.UpnpIgdController.java

private Map<String, String> parseResponseXml(String expectedTagName, byte[] data) {
    try {/*from www  . j a v  a 2  s . c o m*/
        MessageFactory factory = MessageFactory.newInstance();
        SOAPMessage soapMessage = factory.createMessage(new MimeHeaders(), new ByteArrayInputStream(data));

        if (soapMessage.getSOAPBody().hasFault()) {
            StringWriter writer = new StringWriter();
            try {
                Transformer transformer = TransformerFactory.newInstance().newTransformer();
                transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
                transformer.transform(new DOMSource(soapMessage.getSOAPPart()), new StreamResult(writer));
            } catch (IllegalArgumentException | TransformerException | TransformerFactoryConfigurationError e) {
                writer.append("Failed to dump fault: " + e);
            }

            throw new ResponseException(writer.toString());
        }

        Iterator<SOAPBodyElement> responseBlockIt = soapMessage.getSOAPBody()
                .getChildElements(new QName(serviceType, expectedTagName));
        if (!responseBlockIt.hasNext()) {
            throw new ResponseException(expectedTagName + " tag missing");
        }

        Map<String, String> ret = new HashMap<>();

        SOAPBodyElement responseNode = responseBlockIt.next();
        Iterator<SOAPBodyElement> responseChildrenIt = responseNode.getChildElements();
        while (responseChildrenIt.hasNext()) {
            SOAPBodyElement param = responseChildrenIt.next();
            String name = StringUtils.trim(param.getLocalName().trim());
            String value = StringUtils.trim(param.getValue().trim());

            ret.put(name, value);
        }

        return ret;
    } catch (IllegalArgumentException | IOException | SOAPException | DOMException e) {
        throw new IllegalStateException(e); // should never happen
    }
}