Dom4j selectNodes(arg)不提供节点列表 [英] Dom4j selectNodes(arg) don't give list of nodes

查看:143
本文介绍了Dom4j selectNodes(arg)不提供节点列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在java中使用DOM4j进行XML工作,
我的xml是这样的:

I am using DOM4j for XML work in java, my xml is like this:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<abcd name="ab.catalog" xmlns="http://www.xyz.com/pqr" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.xyz.com/pqr ./abc.xyz.xsd">             
<efg>
......
</efg>
<efg>
.....
</efg>
</abcd>

然后,

List<Node>list = document.selectNodes("/abcd/efg");

获得列表零的大小。我觉得这是由于xml中指定的命名空间。
我尝试了很多但是没有成功。

gets the size of list zero. I feel it's due to namespace specified in the xml. I tried a lot but cn't get success.

推荐答案

XPath表达式中的未加前缀的元素名称指的是那些元素不在命名空间中 - 它们没有考虑在文档上声明的默认 xmlns =...命名空间。您需要在XPath引擎中声明名称空间的前缀,然后在表达式中使用该前缀。这是一个受 DOM4J javadocs

Unprefixed element names in XPath expressions refer to elements that are not in a namespace - they do not take account of the "default" xmlns="..." namespace declared on the document. You need to declare a prefix for the namespace in the XPath engine and then use that prefix in the expression. Here is an example inspired by the DOM4J javadocs:

Map uris = new HashMap();
uris.put("pqr", "http://www.xyz.com/pqr");
XPath xpath = document.createXPath("/pqr:abcd/pqr:efg");
xpath.setNamespaceURIs(uris);
List<Node> nodes = xpath.selectNodes(document);

这篇关于Dom4j selectNodes(arg)不提供节点列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆