HOME       >>       Programming

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


Vyoma

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?


VIEW DESKTOP VERSION REGISTERGET FREE HOSTING

Xisto.com offers Free Web Hosting to its Members for their participation in this Community. We moderate all content posted here but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 2001-2019 by Xisto Corporation. All Rights Reserved.