user1158745
user1158745

Reputation: 2490

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:

<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.

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.

Upvotes: 0

Views: 90

Answers (1)

Dabbler
Dabbler

Reputation: 9863

You're not missing much - just a single character ;-).

What you need is //param[@name='rootDir']/@value.

Upvotes: 2

Related Questions