blo*_*824 3 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/ws/rest/v1">
<nodeA>text</nodeA>
<nodeB>
<nodeb1>
<nodeb1a>
<nodeTest>hello</nodeTest>
</nodeb1a>
</nodeb1>
</nodeB>
</Response>
Run Code Online (Sandbox Code Playgroud)
我正在使用Springs rest模板来检索数据.
这是我使用xpath表达式获取节点值的地方:
xpathTemplate.evaluate("base:*", xmlSource, new NodeMapper() {
public Object mapNode(Node node, int i) throws DOMException {
Element response = (Element) node;
System.out.println("hi: " + response.getTextContent());
Run Code Online (Sandbox Code Playgroud)
我将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);
Run Code Online (Sandbox Code Playgroud)
使用表达式base:*为我提供了Response节点,我可以使用.getChildNodes向下钻取到子节点,并为每个子节点创建节点列表.
我想直接用xpath表达式获取hello值但是我无法使用除了base之外的任何东西:*.有人可以用我的表达来帮助我吗?
谢谢
小智 5
这个XPath表达式:
/base:Response/base:nodeB/base:nodeb1/base:nodeb1a/base:nodeTest
Run Code Online (Sandbox Code Playgroud)
假设base绑定了http://schemas.test.com/search/local/ws/rest/v1名称空间URI.
| 归档时间: |
|
| 查看次数: |
2447 次 |
| 最近记录: |