繁体   English   中英

Java中的xPath表达式

[英]xPath expression in Java

我有以下xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<include template="template.xml">
      <param name="view" value="exampleValue"/>
      <param name="table" value="exampleValue"/>
      <param name="title" value="exampleValue"/>
      <param name="addTitle" value="exampleValue"/>
      <param name="permission" value="exampleValue"/>
      <param name="icon" value="exampleValue"/>
    </include> 

现在,我想在我的Java项目中使用xPath来检查是否有一个<include> -node带有名为template的属性,并且子节点是否包含name -attribute和附带的value

我尝试了这个:

String expression ="//include[@template]";                                                                          //Expression;
NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);


for (int i = 0; i < nodeList.getLength(); i++) 
{
  String match = nodeList.item(i).getAttributes().getNamedItem("value").toString();
  String match2 = nodeList.item(i).getAttributes().getNamedItem("template").toString();
  System.out.println(match);
  System.out.println(match2);
}

试试这个/include[@template]/*[@name][@value]

这将在包含元素/节点下为您提供包含名称和值的节点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM