Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中使用XPath解析SOAP消息_Java_Xml_Soap - Fatal编程技术网

在Java中使用XPath解析SOAP消息

在Java中使用XPath解析SOAP消息,java,xml,soap,Java,Xml,Soap,在根节点上运行此操作非常好,ParentNode将打印到控制台 但是,设置将我的xPath计算替换为以下内容: NodeList list=(NodeList)xPath.compile(“/ParentNode”).evaluate(文档,XPathConstants.NODESET) 结果是一个空列表。我认为它与名称空间有关,因此我将查询替换为以下内容: NodeList list=(NodeList)xPath.compile(“/*[name()='ParentNode']”)。eval

在根节点上运行此操作非常好,
ParentNode
将打印到控制台

但是,设置将我的xPath计算替换为以下内容:

NodeList list=(NodeList)xPath.compile(“/ParentNode”).evaluate(文档,XPathConstants.NODESET)

结果是一个空列表。我认为它与名称空间有关,因此我将查询替换为以下内容:

NodeList list=(NodeList)xPath.compile(“/*[name()='ParentNode']”)。evaluate(文档,XPathConstants.NODESET)

这似乎很有效。我的问题是,如何正确设置名称空间上下文,以便在每个节点周围没有
name()=…
的情况下使用xPath查询?我是否需要使用DocumentBuilder工厂并将其名称空间感知设置为true?如果是这样,我如何将此SOAP消息提供给该工厂?

正如建议的那样,在xPath查询中添加任意前缀是正确解析名称空间的诀窍。因此,以下查询工作正常:

XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xPath = xPathFactory.newXPath();
xPath.setNamespaceContext(new NamespaceContext() {
      @Override
      public String getNamespaceURI(String prefix) {
        return "http://namespace";
      }

      @Override
      public String getPrefix(String namespaceURI) {
        return null;
      }

      @Override
      public Iterator getPrefixes(String namespaceURI) {
        System.out.println(namespaceURI);
        return null;
      }
    });

SOAPBody body = soapMessage.getSoapBody();
Document document = body.extractContentAsDocument();

NodeList list = (NodeList)xPath.compile("/").evaluate(document, XPathConstants.NODESET);
Node node = list.item(0);
System.out.println(node.getFirstChild().getNodeName());

xPath.compile(“/whater:ParentNode”)
?恐怕我不明白,whater标记是什么?如果这是您的意思,那么所需名称空间似乎没有前缀。
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xPath = xPathFactory.newXPath();
xPath.setNamespaceContext(new NamespaceContext() {
      @Override
      public String getNamespaceURI(String prefix) {
        return "http://namespace";
      }

      @Override
      public String getPrefix(String namespaceURI) {
        return null;
      }

      @Override
      public Iterator getPrefixes(String namespaceURI) {
        System.out.println(namespaceURI);
        return null;
      }
    });

SOAPBody body = soapMessage.getSoapBody();
Document document = body.extractContentAsDocument();

NodeList list = (NodeList)xPath.compile("/").evaluate(document, XPathConstants.NODESET);
Node node = list.item(0);
System.out.println(node.getFirstChild().getNodeName());
NodeList list = (NodeList)xPath.compile("/arbitraryprefix:ParentNode").evaluate(document, XPathConstants.NODESET);