java - 解析 SOAP 响应返回 null

标签 java xml parsing soap

String response = "<?xml version='1.0'?><soap:Envelope xmlns:soap='http://www.w3.org/2003/05/soap-envelope'><soap:Body><exch:Response xmlns:exch='http://applicant.ffe.org/exchange/1.0'>...</exch:Response></soap:Body></soap:Envelope>";

DocumentBuilderFactory dbf = null;
DocumentBuilder db = null;
org.w3c.dom.Document document = null;
try {
   dbf = DocumentBuilderFactory.newInstance();
   db = dbf.newDocumentBuilder();
   InputSource is = new InputSource(new ByteArrayInputStream(response.getBytes("UTF-8")));
   document = db.parse(is);

 } catch(ParserConfigurationException e){}
   catch(SAXException e){}

文档返回 null。我尝试了不同的方法来传递给 InputSource,但文档仍然返回 null。知道为什么会发生这种情况吗?

最佳答案

我刚刚尝试获取元素名称和值。

        try {
                   dbf = DocumentBuilderFactory.newInstance();
                   db = dbf.newDocumentBuilder();
                   InputSource is = new InputSource(new ByteArrayInputStream(response.getBytes("UTF-8")));
                   document = db.parse(is);
                   System.out.println(document);//here we get null;
                       System.out.println(document.getNodeName());//here we get document;
                      for(int i =0 ; i<document.getChildNodes().getLength();i++)
                   System.out.println(document.getChildNodes().item(i).getChildNodes().item(i).getNodeName());
                }


    Output :

    [#document: null]
    document
    soap:Body

要解析 SOAPResponse,我们可以javax.xml.soap.*,这可能需要您遍历对象 xml 树。无论如何,我们可能需要解析 SOAP Body 中的元素。我们可以使用 DOM 格式来解析这些非常简单的方式。

关于java - 解析 SOAP 响应返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20184407/

相关文章:

java - 在 JSP/Servlet 中创建和处理多个 session

java - 异常: "Invalid action number found in internal parse table." Polyglot Exception

c# - 使用 C# 标记自定义文本文件格式文件

java - 我的动画按钮显示的时间不够长

xml - XSL 格式和计算

java - Spring 如何从类路径中找到 XSD 文件

mysql - 如何修复MySQL错误#1064?

java - Desfire - Ndef 应用程序命令 : get security exception

java - 使 Gson 在使用重复键解析 JSON 时抛出异常

java - 为什么我的学生字符串没有打印出来?