wood burning stoves 2.0*
The moose likes XML and Related Technologies and the fly likes How to load XML file to hashmap? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "How to load XML file to hashmap?" Watch "How to load XML file to hashmap?" New topic
Author

How to load XML file to hashmap?

Anthony Karta
Ranch Hand

Joined: Aug 09, 2004
Posts: 342
What is the easy way to load XML file to java hashmap/objects?
is Castor a good library? I need to check attribute as well such as

<country id="us">
<statement type="1">
etc

thanks
tony


SCJP 5
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12759
    
    5
Exactly what do you want to do with "java hashmap/objects" after you get them?
Are you talking about creating one or many objects?
You need to expand your description of the problem before you can get more detailed help. It may well be that the standard Java xml library has all you need but we cant tell at this point.

Bill
Anthony Karta
Ranch Hand

Joined: Aug 09, 2004
Posts: 342
Thanks William.

I made the xml structure simpler like below, id is unique string.

<unit id="dgs">
<name>Digital Solution</name>
<email>sam@aol.com.au</email>
<style>dgs.xslt</style>
</uit>
<unit id="xxx">
...
</uit>
etc

I want to load particular id as java object, then use getter method to get its values (name, email, stylsheet etc).

hope that make it clear.
any advice welcome.
thanks again
tony
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Then you don't really need a hash map, right? A structured Java object containing those fields would work better. In this case you want one of the Java-XML binding tools like JAXB or Castor (or quite a few others).
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12759
    
    5
Here is how I tackled a similar problem (using your xml names for example.)

Assuming this is the basic item:
<unit id="dgs">...etc </unit>

1. With DOM in memory, I get a NodeList of all elements named "unit"
2. Iterating through the nodelist - for each unit Element, get the id attribute value.
3. Store the Element in a Hashmap with the id value as key.

Now you can look up the Element by id and do further retrieval of values that are children of the Element. This uses the Element object that the parser creates rather than a separate Java object.

The Java API contains lots of tools for locating and pulling values out of XML objects, you might find it convenient to use XPath (javax.xml.xpath package) in Java 1.5. You could even use XPath directly on the DOM and never mess with Hashmaps at all.

My recent article on a quick intro to XPath.

If you are more comfortable with Java objects, like Paul says, there are LOTS of toolkits. A confusing array of toolkits really.

Bill
 
 
subject: How to load XML file to hashmap?
 
Similar Threads
How to show an xml content in a Jsp page?
XML to HashMap......
creating a hashmap of hashmap using xml
instantiating Java classes on server startup
hashmaps to xml and back