com.billpringle.utils.wrputils
Class WrpXmlMap

java.lang.Object
  extended by com.billpringle.utils.wrputils.WrpXmlMap

public class WrpXmlMap
extends java.lang.Object

Class that creates a HashMap from a simple XML file

This class will parse an XML file with simple name / value pairs and create a HashMap where the value(s) can be accessed by the name and the value being a Vector of String values.

For example, consider the file sample.xml:

 <foo>
 <user>John Smith</user>
 <password>foobar</password>
 <color>red</color>
 <color>blue</color>
 </foo>
 

This class will read the file, ignore the root element (foo) and create HashMap entries for user, password, and color. The user and password entries will return a Vector with a single value, while the color tag will be a Vector with two values.

The easiest way to use this class is to specify the name of the XML file to be parsed in the Constructor. Another way is to call the parseFile(fname) method.

An example session:

 // parse XML file
 WrpXmlMap map = new WrpXmlMap("sample.xml"); 
 // get and display the user value
 Vector vals = map.getValues("user");
 System.out.println("User name: " + vals[0]);
 // get and display the colors
 vals = map.getValues("color");
 System.out.print("Colors:");
 for (int i=0; i<vals.size(); i++)
 { // print all color values
          System.out.println(vals.get(i));
 }
  // parse another dictionary type file
  // re-using the same map
  map.parseFile("dictionary.xml");
  // we can now use getValues(key) to get the values
  // from the new XML file
 

This will produce the following output:

 User Name: John Smith
 Colors:
     red
     blue
 
Creative Commons License Creative Commons License Symbols Unless noted otherwise, all materials available for download from my site are copyrighted by Bill Pringle, and are licensed under a Creative Commons License.

Author:
Bill Pringle

Nested Class Summary
 class WrpXmlMap.XmlMapContentHandler
          XML Event Handler for WrpXmlMap class This class responds as the various XML elements are being read.
 
Field Summary
(package private)  boolean firstElement
          flag to ignore the root element
(package private)  java.util.HashMap<java.lang.String,java.util.Vector<java.lang.String>> map
          name / value map
private  java.lang.String thisKey
          used by XML parser to save current tag name
protected  java.lang.String xmlFile
          name of configuration file
 
Constructor Summary
WrpXmlMap()
          Default constructor
WrpXmlMap(java.lang.String configFile)
          Constructor that specifies the configuration file
 
Method Summary
private  java.lang.String convertToFileURL(java.lang.String filename)
          Convert file name to actual File URL
 java.lang.String getValue(java.lang.String key)
          Generate single value associated with key
 java.util.Vector<java.lang.String> getValues(java.lang.String key)
          Get the list of values associated with a key.
 java.lang.String getXmlFile()
           
static void main(java.lang.String[] args)
           
 void parseFile()
          This method can be called to parse the XML file
 void parseFile(java.lang.String xmlFile)
           
private  java.util.HashMap<java.lang.String,java.util.Vector<java.lang.String>> parseXmlFile(java.lang.String fname)
           
 void printValues(java.io.PrintStream out)
          Print the map names and values to the print stream
 void setXmlFile(java.lang.String xmlFile)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

thisKey

private java.lang.String thisKey
used by XML parser to save current tag name


firstElement

boolean firstElement
flag to ignore the root element


xmlFile

protected java.lang.String xmlFile
name of configuration file


map

java.util.HashMap<java.lang.String,java.util.Vector<java.lang.String>> map
name / value map

Constructor Detail

WrpXmlMap

public WrpXmlMap()
Default constructor

If this constructor is used, the parseFile(fname) method should be used to parse the XML file


WrpXmlMap

public WrpXmlMap(java.lang.String configFile)
          throws java.io.IOException
Constructor that specifies the configuration file

If this method is used, the specified XML file will be parsed.

Parameters:
configFile -
Throws:
java.io.IOException
Method Detail

parseFile

public void parseFile()
               throws java.io.IOException
This method can be called to parse the XML file

Throws:
java.io.IOException

convertToFileURL

private java.lang.String convertToFileURL(java.lang.String filename)
Convert file name to actual File URL

Parameters:
filename - name of file
Returns:
file URL

parseXmlFile

private java.util.HashMap<java.lang.String,java.util.Vector<java.lang.String>> parseXmlFile(java.lang.String fname)
                                                                                     throws java.io.IOException
Throws:
java.io.IOException

parseFile

public void parseFile(java.lang.String xmlFile)
               throws java.io.IOException
Throws:
java.io.IOException

getXmlFile

public java.lang.String getXmlFile()
Returns:
the configFile

setXmlFile

public void setXmlFile(java.lang.String xmlFile)
Parameters:
xmlFile - the configFile to set

printValues

public void printValues(java.io.PrintStream out)
Print the map names and values to the print stream

Parameters:
out - PrintStream to use for display

getValues

public java.util.Vector<java.lang.String> getValues(java.lang.String key)
Get the list of values associated with a key.

Each key can have any number of values associated with it. This method will return all the values associated with the key. If the key isn't present, null will be returned.

Parameters:
key - string under which values are stored
Returns:
list of values or null if not present

getValue

public java.lang.String getValue(java.lang.String key)
Generate single value associated with key

This method is a convenience function for keys that should have a single value. If more than one value is present, the first is returned.

Parameters:
key - string key
Returns:
single or first value; null if not present

main

public static void main(java.lang.String[] args)
Parameters:
args -