• 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

hashmaps to xml and back

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok the title says it all really; what i need is a method/class that will write a hashmap to an xml file in the form

<entry key="key">value</entry> or
<entry key="key" value="value" />

(im not that bothered about doctypes etc, as long as i can load the data to the hashmap when the program starts and then save the hashmap back to the file once the program has run)

i really dont have any experience with using xml in java, so any help you could give would be much appreciated
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could use betwixt (http://jakarta.apache.org/commons/betwixt/):

A map like:


will produce:


Here is a sample (I used one from Jakarta and used a map instead of a bean):


You'll need at least:
- commons-betwixt
- commons-collections
- commons-digester
- commons-logging
- commons-beanutils
 
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
In Java 1.5 the java.util.Properties class got a loadFromXML and a storeToXML method. Properties is an extension of Hashtable and implements Map.
Perhaps that will handle your problem without going to another library.
Bill
 
colin mcculloch
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by William Brogden:
In Java 1.5 the java.util.Properties class got a loadFromXML and a storeToXML method. Properties is an extension of Hashtable and implements Map.
Perhaps that will handle your problem without going to another library.
Bill



i did try this already but i had a few problems, first properties only stores String, String. my map is String, integer. id rather not have to go through converting the integers to srtings and vice versa

the second problem i had (im guessing) is with the way i implemented the code, my xml always turned out like this




Satou: i had a look at your example too, but in my code i define the hashmap:

i take it this wont make any difference to the example you posted?
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I would do it this way, to keep it simple:

1. Create a DOM document



2. Visit every element in the map and convert it to a node:



3. Write the DOM structure to a file:



I hope this helps!
 
colin mcculloch
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
id say Edwins suggestion looks best, but could you please tell me which parts of the API i have to import to use this?
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With JDK 1.5. I think it should work on JDK 1.4 too.

 
colin mcculloch
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
had an ulber post here before, but luckily (all be it after several hours,) i got the problem fixed. i do however have a few more questions on the subject,

first, how do i load the xml file back into a hashmap, and
second, the xml file generated by this example comes out all in one long line rather than being nicely spaced and indented, will this affect the functionality of the program, and is it possible to correct this?
[ June 09, 2006: Message edited by: colin mcculloch ]
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The appearance of the text will be an issue only if you pretend to read it in a text editor. If you open the XML with your browser it will be properly displayed. There are XML beautifiers which could solve this issue, for instance, in my Eclipse editor I could reformat the text to look human-readable.

As for reading the text back from the file, you could:



And you do the DOM structure parsing.

Good luck.
[ June 09, 2006: Message edited by: Edwin Dalorzo ]
 
colin mcculloch
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks to everyone who posted here hopefully i'll be able to figure the rest out for myself. will post back again if not
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good for you, buddy!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic