Java中的简单dom4j解析-无法访问子节点

Java中的简单dom4j解析-无法访问子节点,java,xml,xpath,dom4j,Java,Xml,Xpath,Dom4j,我知道这很容易,我花了一整天的时间敲打我的头。我有这样一个XML文档: <WMS_Capabilities version="1.3.0" xmlns="http://www.opengis.net/wms"> <Service> <Name>WMS</Name> <Title>Metacarta WMS VMaplv0</Title> </Service> <Capability> <Laye

我知道这很容易,我花了一整天的时间敲打我的头。我有这样一个XML文档:

<WMS_Capabilities version="1.3.0" xmlns="http://www.opengis.net/wms">
<Service>
<Name>WMS</Name>
<Title>Metacarta WMS VMaplv0</Title>
</Service>
<Capability>
<Layer>
<Name>Vmap0</Name>
<Title>Metacarta WMS VMaplv0</Title>
<Abstract>Vmap0</Abstract>
...

等等,但它总是返回null。我猜这与名称空间有关,但我所要做的只是获得的每个层节点的名称和标题文本值。任何人都可以提供帮助:

我相信Node.selectSingleNode使用空命名空间上下文计算提供的XPath表达式。因此,无法通过名称访问没有命名空间的节点。必须使用诸如*[local name='name']之类的表达式。如果您想要/需要名称空间上下文,请通过XPath对象执行XPath表达式。

谢谢大家的帮助。这是迈克尔·凯给我的最后线索。。。我需要使用当前节点的相对路径,包括名称空间URI,并从我正在迭代的当前节点的上下文中选择:

Map<String, String> uris = new HashMap<String, String>();
uris.put("wms", "http://www.opengis.net/wms");
XPath xpath1 = doc.createXPath("//wms:Layer");
xpath1.setNamespaceURIs(uris);
List nodes1 = xpath1.selectNodes(doc);

for (Iterator<?> layerIt = nodes1.iterator(); layerIt.hasNext();) {
    Node node = (Node) layerIt.next();
    XPath nameXpath = node.createXPath("./wms:Name");
    nameXpath.setNamespaceURIs(uris);
    XPath titleXpath = node.createXPath("./wms:Title");
    titleXpath.setNamespaceURIs(uris);
    Node name = nameXpath.selectSingleNode(node);
    Node title = titleXpath.selectSingleNode(node);
}

不幸的是,当我使用*[local name='name']时,它仍然返回null。。。ie节点名称=节点。选择SingleNode*[local name='name'];我不知道如何正确使用xpath对象。当我在过去这样做时,它总是访问结果中的第一层对象。考虑到我正在迭代的当前节点,我不知道如何使用XPath对象。但是xpath.selectNodes接受一个节点参数,该参数用作计算xpath表达式的上下文项。感谢您的帮助-您肯定让我走上了正确的道路,因此我将投票表决您的答案。请尝试node.selectSingleNode*:Name。
name = node.selectSingleNode("./wms:Name");
name = node.selectSingleNode("wms:Name");
name = node.selectSingleNode("Name");
Map<String, String> uris = new HashMap<String, String>();
uris.put("wms", "http://www.opengis.net/wms");
XPath xpath1 = doc.createXPath("//wms:Layer");
xpath1.setNamespaceURIs(uris);
List nodes1 = xpath1.selectNodes(doc);

for (Iterator<?> layerIt = nodes1.iterator(); layerIt.hasNext();) {
    Node node = (Node) layerIt.next();
    XPath nameXpath = node.createXPath("./wms:Name");
    nameXpath.setNamespaceURIs(uris);
    XPath titleXpath = node.createXPath("./wms:Title");
    titleXpath.setNamespaceURIs(uris);
    Node name = nameXpath.selectSingleNode(node);
    Node title = titleXpath.selectSingleNode(node);
}