It's not a secret anymore!
The moose likes Java in General and the fly likes get java object and display 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 "get java object and display in XML format" Watch "get java object and display in XML format" New topic

get java object and display in XML format

saket gupta

Joined: Nov 09, 2004
Posts: 12

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

and then we are iterating from HashMap and storing in XML format

we are storing field type , field name and field value in hashmap and output like this
<?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>


what can i use for output like this ......
currently i m using StringBuffer for storing and printing in this 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) + ">");


waiting for reply
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
i'm use DOM4j to create XML document.

Java Developer, Thailand
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
Basiscally, you might consider using JAXP (come with J2SE) or Xerces (developed by Apache) to create XML documents.

And they provide APIs for you to convert it into a "printable" format.


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8927

Check out the Castor XML mapping tool<map-to>-element

I agree. Here's the link:
subject: get java object and display in XML format
It's not a secret anymore!