java - 无法从 Soap 响应中的节点检索文本内容

标签 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 中进行测试,并且还成功打印了响应,因此该值为 null 是没有意义的,除非我没有正确处理响应。我该怎么办?

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)

最佳答案

您会得到 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();
    }

}

关于java - 无法从 Soap 响应中的节点检索文本内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44050002/

相关文章:

java - 是否可以将 Integer.MAX_VALUE 传递给elasticsearch而不影响性能?

java - 需要帮助破译一个小堆栈跟踪 : which kind of WS am I using?

java - Glassfish 基本身份验证错误

播放某些 mp3 文件时 JavaFX 抛出 ERROR_MEDIA_INVALID

java - IntelliJ "navigate declaration"可以配置为支持拆分编辑器吗

java - Java反射“调用”方法的行为不一致

soap - 不能将 HTTPS 与 ServerXMLHTTP 对象一起使用

java - jaxb xjc -wsdl 命令未知位置

java - 在javaFX中调整 TableView 中的列大小时如何获取列宽和名称

Javafx仿射变换