简体   繁体   English

java Xpath无法获取属性值

[英]java Xpath unable to get attribute value

I can't seem to figure out how to get the value from an attribute. 我似乎无法弄清楚如何从属性获取值。

The xml looks like this: xml看起来像这样:

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
 <appender >
     <param name="rootDir" value="C:/logs" />
 </appender>

The If statement finds the name that's equal to file, I just cannot figure out how to pull out the value for rootDir. If语句找到的名称与file相等,我只是无法弄清楚如何提取rootDir的值。

XPath xp = XPathFactory.newInstance().newXPath();
NodeList nl = (NodeList) xp.compile("//appender").evaluate(d, XPathConstants.NODESET);
if (xp.compile("./@name").evaluate(nl.item(i)).equals("file")) {
    XPathExpression expr = xp.compile("//param[name='rootDir']/@value");
    NodeList nodes = (NodeList) expr.evaluate(d, XPathConstants.NODESET);
    for (int x = 0; x < nodes.getLength(); x++) {
        System.out.println("attribute is : " + nodes.item(x).getNodeValue());
    }
}

Thanks. 谢谢。

You're not missing much - just a single character ;-). 您不会错过太多-仅需一个字符;-)。

What you need is //param[@name='rootDir']/@value . 您需要的是//param[@name='rootDir']/@value

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

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