aspose file tools*
The moose likes Java in General and the fly likes hashmaps to xml and back Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "hashmaps to xml and back" Watch "hashmaps to xml and back" New topic
Author

hashmaps to xml and back

colin mcculloch
Greenhorn

Joined: Apr 05, 2006
Posts: 8
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
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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


[My Blog]
All roads lead to JavaRanch
William Brogden
Author and all-around good cowpoke
Rancher

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


Java Resources at www.wbrogden.com
colin mcculloch
Greenhorn

Joined: Apr 05, 2006
Posts: 8
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?
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
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

Joined: Apr 05, 2006
Posts: 8
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

Joined: Dec 31, 2004
Posts: 961
With JDK 1.5. I think it should work on JDK 1.4 too.

colin mcculloch
Greenhorn

Joined: Apr 05, 2006
Posts: 8
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

Joined: Dec 31, 2004
Posts: 961
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

Joined: Apr 05, 2006
Posts: 8
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

Joined: Dec 31, 2004
Posts: 961
Good for you, buddy!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hashmaps to xml and back
 
Similar Threads
JAXB - complex hashmap
Properties class
creating a hashmap of hashmap using xml
Urgent !!! Entity bean JDBC connection Problem !!!
Reading and writing fixed length file