pe4enko
pe4enko

Reputation: 354

Read config with Apache Commons Configuration

I have xml file with config for converter.

<converter>
  <replace>
    <mask from="171, 187, 147, 148" to="34"/>
    <mask from="150, 151" to="45"/>
  </replace>
</converter>

To read this config i'm using Apache Commons Configuration. How i can read tags "mask" to iterate it and process attributes in code?

Upvotes: 1

Views: 702

Answers (1)

pe4enko
pe4enko

Reputation: 354

My solution is to use xpath.

            XPath xpath = XPathFactory.newInstance().newXPath();
        XPathExpression fromAttribute = xpath.compile("@from");
        XPathExpression toAttribute = xpath.compile("@to");

        NodeList list = (NodeList) xpath.evaluate("/converter/replace/mask",
                    ((XMLConfiguration) configuration).getDocument(), XPathConstants.NODESET);

        for (int i = 0; i < list.getLength(); i++) {
            Node node = list.item(i);
            String from = fromAttribute.evaluate(node);
            String to = toAttribute.evaluate(node);

            //...
        }

Upvotes: 1

Related Questions