0

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.

1
  • Please next time pay attention to your indentation. Commented Nov 2, 2015 at 18:59

1 Answer 1

2

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

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.