Jump to content
xisto Community
Vyoma

Use Of Xml Properties File For One Key - Multiple Value Mapping - should I use some other Class?

Recommended Posts

Here is a situation I have encountered. I know I can write a custom code to get the job done, but I was wondering if the java.util.Properties class (or something else) had this inbuilt and I was missing it in the documentation.

 

Situation: Need to load from a file, a set of values (mutliple values) for an associated single key.

 

To illustrate, here is an example:

propfile.properties

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"><properties>	<entry key="fooA">barA1</entry>	<entry key="fooA">barA2</entry>		<entry key="fooB">barB1</entry></properties>

The fooB=barB1 is a trivial case in implementing using the Properties class. But what about the multiple case - fooA=[barA1,barA2] ? How do I get them into a HashMap or something?

 

Here is a independent code snippet that shows my attempt. It shows the trivial case without any problem.

PropPlay.java

import java.util.Properties;import java.io.FileInputStream;public class PropPlay {	/**	 * @param args	 */	public static void main(String[] args) {		System.out.println(" --------------------------------------- ");		System.out.println(" - This is a Java Properties file test - ");		System.out.println(" --------------------------------------- \n");				try {		System.out.println("Loading cfg/propfile.properties");		FileInputStream fis = new FileInputStream("cfg/propfile.properties");		System.out.println("...loaded.\n");				System.out.println("Loading to internal Properties object");		Properties prop = new Properties();		prop.loadFromXML(fis);		System.out.println("...loaded.\n");				System.out.println("Here are the properties:");		prop.list(System.out);				System.out.println("\nGetting properties for fooA: ");		System.out.println(prop.getProperty("fooA"));				} catch (Exception e) {			e.printStackTrace();			System.out.println("Bailing out!!");		}	}}

Output:

---------------------------------------  - This is a Java Properties file test -  --------------------------------------- Loading cfg/propfile.properties...loaded.Loading to internal Properties object...loaded.Here are the properties:-- listing properties --fooA=barA2fooB=barB1Getting properties for fooA: barA2

The one key - multiple values obviously does not work as intended. Any idea how I can get it into a HashMap or some object like that?
Edited by Vyoma (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.