File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes XML proprty file, printout order Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "XML proprty file, printout order" Watch "XML proprty file, printout order" New topic
Author

XML proprty file, printout order

wayne morton
Greenhorn

Joined: May 17, 2012
Posts: 28
I am starting to play with XML files to store my data and i was wondering if someone could explain to me the logic behind the way it adds information to the file.e.g. i have this simple little code


All works fine apart from the order it stores the information in the file.i.e. the XML file looks like this

Why does it store the data in that order and not a, b, c?
I would prefer it did a ,b ,c rather than b, a, c, mainly for clarity reasons when i am checking it has stored the data properly and the first step towards that goal is understanding why it prints in the order it does.

I guess my obvious next question is tips for ordering it or where to find information on how to order it?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18563
    
    8

Because Properties extends Hashmap, which is an unordered map.

And so when you extract data from a Properties object and write it to XML, the order of that data is undefined.

Tips for ordering the data: the easiest way to deal with it is to stop wanting it. However if you absolutely must have your data in a specific order in the properties file, then don't use a Properties object.

(And I'm going to move this post out of the Swing forum because it isn't related to Swing at all.)
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18563
    
    8

Here's another way to look at it:

When you have a properties file, you have two choices. One choice is to maintain it with a text editor, in which case you can put the entries in any way you like. When you read them into a Properties object, they become unordered, but you don't care because the ordering means nothing to the application which uses the properties.

Or you can maintain them with some application, which works with the Properties file and knows what order to put the entries in when it displays them to you. When the Properties object writes them out to the file, they become unordered, but you don't care because you're maintaining them with something which knows what it's doing.

It's when you start using both ideas at the same time that you run into trouble with "unordered" properties. So just pick one of the two approaches and you won't care.
wayne morton
Greenhorn

Joined: May 17, 2012
Posts: 28
Thanks for the reply Paul. It would seem for future reference that using Properties is not the ideal way so i will look at other ways for transferring data in future, for now having it ordered would have just been nice rather than any form of necessity.

Thanks for moving the post also, i thought i put it in a different section than i did.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: XML proprty file, printout order