I need a method that can give me back the value of a specified attribute inside a specified xpath. So for example if we have xpath = /root/foo/body/part[5]/test[3]
and we want the value of the attribute id
inside the test[3]
tag of the xpath then I need to be able to call a method that looks something like this:
public String getAttributeValue(String xpath, String attribute) {
String attributeValue = "xpath/attribute".value();
return attributeValue;
}
You can try with the @
command
//xpath[@attribute]
Note that you can also filter with the @
//xpath[@attribute='filtervalue']
Figured it out...
public String retrieveAttributeValue(Document document, String xpath, String attribute) throws IOException, ParserConfigurationException, SAXException, XPathExpressionException {
XPath xPath = XPathFactory.newInstance().newXPath();
XPathExpression xPathExpression = xPath.compile(xpath + "/" + attribute);
String attributeValue = "" + xPathExpression.evaluate(document, XPathConstants.STRING);
return attributeValue;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.