无法从肥皂响应中的节点检索文本内容

时间:2017-05-18 14:04:20

标签: java soap javafx tableview

这是打印的SOAP响应:

<?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>

提取节点值的方法:

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();
    }

我想要的只是返回此节点<ns0:Full_Name>asdf - Full Name</ns0:Request_ID>的文本内容。

我在SoapUI中进行测试,并且我也成功打印了响应,因此除非我不正确地处理响应,否则该值无效是不合理的。我该怎么办?

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)

1 个答案:

答案 0 :(得分:1)

您收到class cast exception,因为getElementsByTagName()的返回类型为NodeList,而不是Node。因此,您无法向下转换为Node

您应该执行类似

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

由于命名空间,您将在此处遇到其他问题。如果您只是提供标记名称("Full_Name"),则需要确保解析器具有名称空间感知功能,并使用getElementsByTagNameNS("urn:PeopleInterface", nodeValue)。您可以将通配符"*"用于命名空间,该命名空间将匹配任何命名空间。否则,您应该提供完整的标签名称(即执行getNode(responseElementAssignee, "ns0:Full_Name")。)

这是一个具有命名空间感知方法的SSCCE:

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();
    }

}