0

This is the printed SOAP Response:

<?xml version="1.0" encoding="utf-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Body>
        <ns0:Get_People_Operation xmlns:ns0="urn:PeopleInterface" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <ns0:Full_Name>asdf - Full Name</ns0:Full_Name>
        </ns0:Get_People_Operation>
    </soapenv:Body>
</soapenv:Envelope>

Method to extract node value:

String assigneeInput = getNode(responseElementAssignee, "Full_Name");

private static String getNode(Element responseElement, String nodeValue) {
        Node x = (Node) responseElement.getElementsByTagName(nodeValue);
        x.getTextContent();

        // Test list output
        System.out.println("");
        System.out.println(nodeValue + " Value: " + x.toString());
        System.out.println("");

        return x.getTextContent();
    }

All I want is to return the text content of this node <ns0:Full_Name>asdf - Full Name</ns0:Request_ID>.

I'm testing in SoapUI and I'm also printing the response successfully so it doesn't make sense that the value is null, unless of course I'm processing the response improperly. What should I do?

ClassCastException:

java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.DeepNodeListImpl cannot be cast to org.w3c.dom.Node
    at app.controller.TableViewController.printSOAPResponse(TableViewController.java:225)
    at app.controller.TableViewController.initialize(TableViewController.java:67)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at app.Main.start(Main.java:14)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)
Martin Erlic
  • 5,467
  • 22
  • 81
  • 153

1 Answers1

1

You get a class cast exception because the return type of getElementsByTagName() is NodeList, which is not a Node. Hence you cannot downcast to Node.

You should do something like

NodeList nodeList = responseElement.getElementsByTagName(nodeValue);
Node x = nodeList.item(0);

You will have further problems here due to the namespace. If you are simply providing the tag name ("Full_Name"), you need to ensure the parser is namespace-aware, and use getElementsByTagNameNS("urn:PeopleInterface", nodeValue). You can use the wildcard "*" for the namespace, which will match any namespaces. Otherwise you should provide the complete tag name (i.e. do getNode(responseElementAssignee, "ns0:Full_Name").)

Here's a SSCCE with the namespace-aware approach:

import java.io.StringReader;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class XMLParsingTest {

    private static final String XML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
            + "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
            +   "<soapenv:Body><ns0:Get_People_Operation xmlns:ns0=\"urn:PeopleInterface\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
            + "<ns0:Full_Name>asdf - Full Name</ns0:Full_Name>"
            + "</ns0:Get_People_Operation>"
            + "</soapenv:Body>"
            + "</soapenv:Envelope>";

    public static void main(String[] args) throws Exception {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setNamespaceAware(true);
        Document doc = documentBuilderFactory.newDocumentBuilder().parse(new InputSource(new StringReader(XML)));
        Element elt = doc.getDocumentElement();
        System.out.println(getNode(elt, "Full_Name"));
    }

    private static String getNode(Element responseElement, String nodeValue) {

        NodeList nodeList = responseElement.getElementsByTagNameNS("urn:PeopleInterface",nodeValue);
        Node x = nodeList.item(0);

        // Test list output
        System.out.println("");
        System.out.println(nodeValue + " Value: " + x.toString());
        System.out.println("");

        return x.getTextContent();
    }

}
Community
  • 1
  • 1
James_D
  • 201,275
  • 16
  • 291
  • 322
  • This works. Thanks. Even before this, however, my problem was that the ``responseElement`` parameter itself was null, and thus the text content of the node value could not even be registered. Both solutions fixed my issue. Cheers. – Martin Erlic May 19 '17 at 09:52