commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Oliver Heger (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (CONFIGURATION-560) Receiving duplicate list entries for XPATH expression on XML document via org.apache.commons.configuration.HierarchicalConfiguration.configurationsAt
Date Mon, 18 Nov 2013 20:55:21 GMT

    [ https://issues.apache.org/jira/browse/CONFIGURATION-560?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13825771#comment-13825771
] 

Oliver Heger commented on CONFIGURATION-560:
--------------------------------------------

It seems that you ran into a trap here. You created the configuration in the following way:
{code}
XMLconfig = new XMLConfiguration("/tmp/sample.xml");
XMLconfig.load();
{code}

This actually loads the data twice! The constructor already loads the specified file, the
following load() operation loads it again constructing a union of the data already available.
Thus you end up with duplicate entries.

Just remove the additional load() call. This should solve the problem.

> 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)

Mime
View raw message