如何重写document.getElementsByTagName(); XPath吗?

时间:2019-01-22 11:07:06

标签: java xml xpath

您能帮助我使用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");
            }
        }

1 个答案:

答案 0 :(得分:0)

要重写:

document.getElementsByTagName("ruleDefinition");

可以使用 XPath 的内部消息:

document.evaluate('//ruleDefinition', document, null, XPathResult.ANY_TYPE, null);