|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.billpringle.utils.wrputils.WrpXmlMap
public class WrpXmlMap
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 Vectorvals = 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
![]() |
![]() |
Unless noted otherwise, all materials available for download from my site are copyrighted by Bill Pringle, and are licensed under a Creative Commons License. |
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 |
---|
private java.lang.String thisKey
boolean firstElement
protected java.lang.String xmlFile
java.util.HashMap<java.lang.String,java.util.Vector<java.lang.String>> map
Constructor Detail |
---|
public WrpXmlMap()
If this constructor is used, the parseFile(fname) method should be used to parse the XML file
public WrpXmlMap(java.lang.String configFile) throws java.io.IOException
If this method is used, the specified XML file will be parsed.
configFile
-
java.io.IOException
Method Detail |
---|
public void parseFile() throws java.io.IOException
java.io.IOException
private java.lang.String convertToFileURL(java.lang.String filename)
filename
- name of file
private java.util.HashMap<java.lang.String,java.util.Vector<java.lang.String>> parseXmlFile(java.lang.String fname) throws java.io.IOException
java.io.IOException
public void parseFile(java.lang.String xmlFile) throws java.io.IOException
java.io.IOException
public java.lang.String getXmlFile()
public void setXmlFile(java.lang.String xmlFile)
xmlFile
- the configFile to setpublic void printValues(java.io.PrintStream out)
out
- PrintStream to use for displaypublic java.util.Vector<java.lang.String> getValues(java.lang.String 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.
key
- string under which values are stored
public java.lang.String getValue(java.lang.String 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.
key
- string key
public static void main(java.lang.String[] args)
args
-
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |