Hi, Newbie question. I am trying to get in the habit of using an XML file for config files rather than a properties file. I'm working through the below easy example. I would like to parse an XML file and put the content into a hashmap. I have some question on my handler class.
Here's my question handler class. How do I parse the XML file and put into a map? In the startElement() method, do I need @params? I think the endElement() method should be pretty straight forward. No @params. All examples pass @params but these are call back methods. I just want the XML file content in a map. All examples seem to be a little overkill for my question.
XML file to parse
Thanks for the help. If you know a site with a simple walk through of writing a handler that would be great too. CA [ March 26, 2004: Message edited by: Steve Rodgers ] [ March 27, 2004: Message edited by: Steve Rodgers ] [ March 28, 2004: Message edited by: Steve Rodgers ]
For a small config file I would consider using a DOM parser instead of SAX. If you use SAX you will have to provide for the characters() handler if you want to get the "steve_server" out of the
Joined: Jul 03, 2003
Thanks Bill. I will make a note. DOM for smaller XML config files. I can see avoiding the charactors() method would be convenient. I'm going to try and finish this example though. Going forward I'll implement your DOM advice.
Steve, remember that the SAX parser might fragment "steve_server" into two separate calls to characters(). In practice, this is very unlikely to happen to such small pieces of text but it's a chance nevertheless. You should basically use a StringBuffer to append to whenever characters() gets invoked and then, upon endElement(), convert whatever you have in the StringBuffer into the actual "element value".
Now I have to; 1. Remember to use the DOM parser instead of SAX for small tasks 2. Use the StringBuffer to avoid potential issues with the charators() method 3. Figure out how to parse and add to a map What am I missing in the startElement() method? I know it must be obvious I just don't see it. [ March 28, 2004: Message edited by: Steve Rodgers ]
Author and all-around good cowpoke
Joined: Mar 22, 2000
Well, for one thing, your CONFIG constant is upper case and your <config> element is lower case so they never match. Bill
Joined: Jul 03, 2003
Oooh. Good catch Mr. Brogden. Good catch. I have some more to do but I've sorted out the main idea. Thanks for the tips. This wasn't easier than a property file but that's just b/c this was the first attempt. Thanks again.