wood burning stoves 2.0*
The moose likes XML and Related Technologies and the fly likes XML handler --> populate Hashmap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XML handler --> populate Hashmap" Watch "XML handler --> populate Hashmap" New topic
Author

XML handler --> populate Hashmap

Steve Rodgers
Greenhorn

Joined: Jul 03, 2003
Posts: 21
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 ]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
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

element.
Bill
Steve Rodgers
Greenhorn

Joined: Jul 03, 2003
Posts: 21
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.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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".


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Steve Rodgers
Greenhorn

Joined: Jul 03, 2003
Posts: 21
StringBuffer huh. This easy example is growing
I was able to tweak my code a bit and now I can get the element but not the attribute returned (I've temporarily given up the map).

This returns:
Step: startElement() config stringEleme
Step: startElement() servername stringE
endElement() method
endElement() method

rc_config.xml parsed - found Orders:
XML file

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 ]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
Well, for one thing, your CONFIG constant is upper case and your <config> element is lower case so they never match.
Bill
Steve Rodgers
Greenhorn

Joined: Jul 03, 2003
Posts: 21
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.

XML file:
 
Consider Paul's rocket mass heater.
 
subject: XML handler --> populate Hashmap
 
Similar Threads
problems with JAXP
problems with JAXP
CPU utilization
Searching using SAX !!
problems with JAXP