您能帮助我使用XPath重写以下代码吗?
final Document document = XMLUtil.buildDocFromString(xmlString);
final NodeList ruleNodeList = document.getElementsByTagName("ruleDefinition");
for (int i = 0; i < ruleNodeList.getLength(); i++) {
Node currentElement = ruleNodeList.item(i);
currentElement.getAttributes().getNamedItem("updatedBy").setNodeValue(user);
}
final NodeList nodeList = document.getElementsByTagName("configuration");
for (int i = 0; i < nodeList.getLength(); i++) {
final Node currentElement = nodeList.item(i);
if (currentElement.getAttributes().getNamedItem("type").getChildNodes().item(0).getNodeValue().equals("flag")) {
currentElement.getChildNodes().item(0).setNodeValue(byPassManualReviewFlag ? "Y" : "N");
}
}
答案 0 :(得分:0)
要重写:
document.getElementsByTagName("ruleDefinition");
可以使用 XPath 的内部消息:
document.evaluate('//ruleDefinition', document, null, XPathResult.ANY_TYPE, null);