Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.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编译可以';具有唯一命名空间的t查询标记_Java_Xml_Xpath - Fatal编程技术网

Java XPath编译可以';具有唯一命名空间的t查询标记

Java XPath编译可以';具有唯一命名空间的t查询标记,java,xml,xpath,Java,Xml,Xpath,我在这里问了很多问题,但似乎没有什么能解决我所面临的问题 我得到了一个带有唯一名称空间的标记。。就那个标签。我引入了XML名称空间上下文,但我的程序仍然返回0个节点 // Create DocumentBuilderFactory for reading xml file DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder =

我在这里问了很多问题,但似乎没有什么能解决我所面临的问题

我得到了一个带有唯一名称空间的标记。。就那个标签。我引入了XML名称空间上下文,但我的程序仍然返回0个节点

// Create DocumentBuilderFactory for reading xml file
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();

        factory.setNamespaceAware(true);

        Document doc = builder.parse(new InputSource(new StringReader(text)));

        // Create XPathFactory for creating XPath Object
        XPathFactory xPathFactory = XPathFactory.newInstance();

        // Create XPath object from XPathFactory
        XPath xpath = xPathFactory.newXPath();

        xpath.setNamespaceContext(new NamespaceContext() {
            public String getNamespaceURI(String prefix) {
                if ("cam".equals(prefix))
                    return "urn:xxx:1.0:logging";
                else if ("soapenv".equals(prefix))
                    return "http://schemas.xmlsoap.org/soap/envelope/";
                else if ("xml".equals(prefix))
                    return XMLConstants.XML_NS_URI;
                return XMLConstants.NULL_NS_URI;
            }

            // This method isn't necessary for XPath processing.
            public String getPrefix(String uri) {
                throw new UnsupportedOperationException();
            }

            // This method isn't necessary for XPath processing either.
            public Iterator getPrefixes(String uri) {
                throw new UnsupportedOperationException();
            }
        });

        // Compile the XPath expression for getting the response
         XPathExpression xPathExpr =
         xpath.compile("/cam:message/cam:response/soapenv:Envelope/soapenv:Header/Info[1]/property[@name='response']/text()");


        // XPath text example : executing xpath expression in java
        // Object result = xPathExpr.evaluate(doc, XPathConstants.NODESET);
        // NodeList nodes = (NodeList) result;
        // int length = nodes.getLength();

        String result1 = (String) xPathExpr.evaluate(doc, XPathConstants.STRING);
XML看起来像

 <cam:message xmlns="urn:xxx:1.0:logging">
         <cam:response>
           <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
               <soapenv:Header xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
                   <Info xmlns="urn:xxx:1.0:xxx.core.xxx.Service1">
                      <property name="response">some value</property>
                 </Info>
                 <Info xmlns="urn:xxx:1.0:xxx.core.xxx.Service2">
                      <property name="response">i am interested in this value</property>
                 </Info>
               </soapenv:Header>
           </soapenv:Envelope>
     </cam:response>
</cam:message>

一些价值
我对这个值感兴趣
因此标记
有自己的名称空间。我的程序没有为此返回值。类似地,还有许多信息标签。我对第二个Info标记感兴趣,它的名称空间以Service2结尾


请帮助

您当前的XPath表达式只匹配带有命名空间uri的信息和属性元素

必须将
urn:…Service2
-命名空间添加到
namespacecoxt
内部类:

public String getNamespaceURI(String prefix) {
    if ("cam".equals(prefix))
       // like abvce
    else if ("s2".equals(prefix))
       return "urn:xxx:1.0:xxx.core.xxx.Service2";
    else
       return XMLConstants.NULL_NS_URI;
}
并相应地限定XPath中的
Info
属性
步骤,使用前缀
s2

xpath.compile("/cam:message/cam:response/soapenv:Envelope/soapenv:Header/s2:Info/s2:property[@name='response']/text()");
除此之外,您的代码和XML文件中还有错误:

a) 在创建DocumentBuilder后,您可以使DocumentBuilderFactory命名空间具有感知性。改为写:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
<cam:message xmlns:cam="urn:xxx:1.0:logging">
    ....
b) XML使用的前缀
cam
,在根元素中没有正确声明。改为写:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
<cam:message xmlns:cam="urn:xxx:1.0:logging">
    ....

....

@wero.我做了你建议的更改。。谢谢但我认为这不管用either@Shiv也许你必须进一步调整你的XPath表达式。我对这个XPath是新手。虽然我的路径看起来简单且合乎逻辑,但不知道为什么它不起作用。有什么帮助吗possible@wero.Thank你。在构建器创建之前添加名称空间感知修复了将issue.wrt更改为xml的问题,我对此没有控制权。但这一改变就解决了我的问题。再次谢谢你