• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

XML handler --> populate Hashmap

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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".
 
Steve Rodgers
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic