aspose file tools*
The moose likes Java in General and the fly likes Display HashMap objects in XML format? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Display HashMap objects in XML format?" Watch "Display HashMap objects in XML format?" New topic
Author

Display HashMap objects in XML format?

saket gupta
Greenhorn

Joined: Nov 09, 2004
Posts: 12
hi all



suppose we are storing all instance variable of class obj in HashMap using reflection

and then we are iterating field type , field name and field value from HashMap and displaying in XML format OUTPUT

<?xml version="1.0" encoding="UTF-8"?>

<root class="One" label=VAR1/>

<int name="int_One" >10</int>

<String name="str_One" >One</String>

<float name="float_one" >1.2</float>

</root>

currently i m using StringBuffer for storing object and display in this above way by hardcode
like this

for (int counter = 0; counter <= xmlfieldnames.size(); counter++) {

if (xmlfieldnames.get("FieldName" + counter) != null) {

xmlStrBuffer.append("\n<" +

xmlfieldtype.get("FieldType" + counter) + " name=\"" +

xmlfieldnames.get("FieldName" + counter) + "\" >" +

xmlfieldvalue.get("FieldValue" + counter) + "</" +

xmlfieldtype.get("FieldType" + counter) + ">");

}



But i want to use XML (DOM /SAX) or JAVAX.xml API for above output insteed of StringBuffer
pl.. send me code or guideline


waiting for reply

bye

saket
Magnus Palm�r
Greenhorn

Joined: Nov 23, 2004
Posts: 7
Have you checked out the JDOM API?
I got started very quickly and I found it much more natural to use than the DOM for instance.

JDOM Homepage

Here is an article to get you started:
Simplify XML programming with JDOM

Note this though
Submitted as JSR-102, JDOM has been approved for eventual inclusion in the core Java platform, with this comment from Sun: "JDOM does appear to be significantly easier to use than the earlier APIs, so we believe it will be a useful addition to the platform." According to the JSR, a 1.0 release might see JDOM's packaging change from "org.jdom" to "javax.xml.tree". While the future certainly looks positive, developers may eventually have to retrofit their code to work with the new version.
 
 
subject: Display HashMap objects in XML format?