Java 需要使用名称空间的xPath表达式的帮助吗

Java 需要使用名称空间的xPath表达式的帮助吗,java,spring,xpath,expression,Java,Spring,Xpath,Expression,我需要一些关于xpath表达式的帮助 我有以下xml文档: <?xml version="1.0" encoding="utf-8"?> <Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.test.com/search/local

我需要一些关于xpath表达式的帮助

我有以下xml文档:

<?xml version="1.0" encoding="utf-8"?>
<Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:xsd="http://www.w3.org/2001/XMLSchema"
          xmlns="http://schemas.test.com/search/local/ws/rest/v1">
    <nodeA>text</nodeA>
    <nodeB>
        <nodeb1>
            <nodeb1a>
                <nodeTest>hello</nodeTest>
            </nodeb1a>
        </nodeb1>
    </nodeB>
</Response>
我将base添加到我的命名空间映射中,如下所示:

HashMap<String, String> nameSpaces = new HashMap<String, String>();
nameSpaces.put("xsi", "http://www.w3.org/2001/XMLSchema-instance");
nameSpaces.put("xsd", "http://www.w3.org/2001/XMLSchema");
nameSpaces.put("base", "http://schemas.test.com/search/local/ws/rest/v1");
((Jaxp13XPathTemplate) xpathTemplate).setNamespaces(nameSpaces);
HashMap namespace=newhashmap();
nameSpaces.put(“xsi”http://www.w3.org/2001/XMLSchema-instance");
nameSpaces.put(“xsd”http://www.w3.org/2001/XMLSchema");
名称空间。put(“base”http://schemas.test.com/search/local/ws/rest/v1");
((Jaxp13XPathTemplate)xpathTemplate);
使用表达式库:*为我提供响应节点,我可以使用.getChildNodes深入到子节点,并为每个子节点创建节点列表

我希望直接使用xpath表达式获取hello值,但除了base:*之外,我无法让它使用任何东西。有人能帮我表达一下吗


谢谢

请尝试以下操作:

xpathTemplate.evaluate("/Response/nodeB/nodeb1/nodeb1a/nodeTest", ...
如果需要有关XPath的更多信息,可以尝试以下方法

我的网站还提供了一个测试xpath查询的工具: 此XPath表达式:

/base:Response/base:nodeB/base:nodeb1/base:nodeb1a/base:nodeTest

假设
base
绑定到
http://schemas.test.com/search/local/ws/rest/v1
名称空间URI。

hello=xpathTemplate.evaluateAsString(“/*[local-name()='nodeTest']”,源代码)

谢谢您的帮助。我不能让它工作。即使我尝试/回应,也没有得到任何结果。base:*是我唯一可以使用的表达方式。我认为这与名称空间有关,但我不确定。还有其他想法吗?太棒了!这很有效。非常感谢。快速提问,以便我更好地理解它。为什么每个节点级别都需要base:?我试过两种方法,没有它就不行。只是不确定,因为它只在主响应节点中使用。@Blong824:不客气。每个名称测试都需要
base:
前缀,因为所有这些元素都属于这样的名称空间URI。
/base:Response/base:nodeB/base:nodeb1/base:nodeb1a/base:nodeTest