Martin Schlegel created CONFIGURATION-560:
---------------------------------------------
Summary: Receiving duplicate list entries for XPATH expression on
XML document via
org.apache.commons.configuration.HierarchicalConfiguration.configurationsAt
Key: CONFIGURATION-560
URL: https://issues.apache.org/jira/browse/CONFIGURATION-560
Project: Commons Configuration
Issue Type: Bug
Affects Versions: 1.10
Reporter: Martin Schlegel
The XPATH expression used selects only one element, but getList and
configurationsAT retrieves the same element twice ... or I am simply not
getting the manual :-)
Please see the simple example code attached below.
Fedora Linux FC 17
Java 1.7
Apache Commons Components used:
commons-collections-3.2.1.jar
commons-configuration-1.10.jar
commons-jxpath-1.3.jar
commons-lang-2.6.jar
commons-logging-1.1.3.jar
XML document "sample.xml":
____________________________________________
<?xml version="1.0" encoding="UTF-8"?>
<config>
<names>
<firstname id="1"> Peter </firstname>
<firstname id="2"> Michael </firstname>
<firstname id="3"> Alex </firstname>
</names>
</config>
____________________________________________
Java code:
____________________________________________
import java.util.List;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.HierarchicalConfiguration;
import org.apache.commons.configuration.XMLConfiguration;
import org.apache.commons.configuration.tree.xpath.XPathExpressionEngine;
public class xmltest {
public static void main(String[] args) {
XMLConfiguration XMLconfig;
try {
XMLconfig = new XMLConfiguration("/tmp/sample.xml");
XMLconfig.load();
XMLconfig.setExpressionEngine(new
XPathExpressionEngine());
System.out.println("Retrieving firstname with id=2 via
configurationsAt() ...");
List<HierarchicalConfiguration> fields =
XMLconfig.configurationsAt("names/firstname[@id='2']");
for (HierarchicalConfiguration sub : fields) {
System.out.println("=> names/firstname[@id='2']
= " + sub.getString("."));
}
System.out.println("\nRetrieving firstname with id=2
via getList() ...");
List<Object> liObjects =
XMLconfig.getList("names/firstname[@id='2']");
for (Object obj : liObjects) {
System.out.println("=> names/firstname[@id='2']
= " + (String) obj);
}
System.out.println("\nRetrieving firstname with id=2
via getString() ...");
System.out.println("=> names/firstname[@id='2'] = " +
XMLconfig.getString("names/firstname[@id='2']"));
}
catch (ConfigurationException e) {
e.printStackTrace();
}
}
}
____________________________________________
Output:
____________________________________________
Retrieving firstname with id=2 via configurationsAt() ...
=> names/firstname[@id='2'] = Michael
=> names/firstname[@id='2'] = Michael
Retrieving firstname with id=2 via getList() ...
=> names/firstname[@id='2'] = Michael
=> names/firstname[@id='2'] = Michael
Retrieving firstname with id=2 via getString() ...
=> names/firstname[@id='2'] = Michael
____________________________________________
--
This message was sent by Atlassian JIRA
(v6.1#6144)