aspose file tools*
The moose likes Java in General and the fly likes Generating Dynamic XML using Reflection for Classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Generating Dynamic XML using Reflection for Classes" Watch "Generating Dynamic XML using Reflection for Classes" New topic
Author

Generating Dynamic XML using Reflection for Classes

Em Aiy
Ranch Hand

Joined: May 11, 2006
Posts: 226
I need to send the changed data from the database (one server to another) and since i am not sure which table -> Bean is gona change, I have decided to send the changed in the format of XML generated at run time



For the generation of this XML i thought to create it via REFLECTION i.e; using reflection i'll get the class name (which will be table-name) and then field name (which will be column-name).

The XML generation class will have a method like



This ParentBean class will be the parent of all bean classes mapped on tables. I am doing this so that i may pass *ANY* class object to the generateXML method and it'll generate XML.

Now i have 2 questions.

1: Is my approach right? or there is any other good way to generate DYNAMIC XML for ANY CLASS at runtime. The xml should have it's column names as CLASS fields etc ...

2: How can i get field names of my child class which is being referenced by parentBean - using reflection.


The difference between <b>failure</b> and <b>success</b> is often being <b>right</b> and being <b>exactly right</b>.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

It sounds that you are looking for JAXB, Java-to-XML Binding (introduction).

You could also have a look at Castor.
Castor is an Open Source data binding framework for Java[tm]. It's the shortest path between Java objects, XML documents and relational tables. Castor provides Java-to-XML binding, Java-to-SQL persistence, and more.

[ June 27, 2008: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Em Aiy
Ranch Hand

Joined: May 11, 2006
Posts: 226
JAXB requires that the XML document you want to access has a schema, and that schema is written in the W3C XML Schema Language

I don't have any schema ... can't i avoid SCHEMA and even then i use JAXB ? is there anyother aproach ?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

Check out XStream.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Em Aiy
Ranch Hand

Joined: May 11, 2006
Posts: 226
Originally posted by Rob Prime:
Check out XStream.


look great. But one problem. Since i am not sure which TABLE OBJECT i am gona have after monitoring the DB. I have decided to have a base table object say



every table object are gona extend from it



now how would i use BaseBean to generate the XML. I don't think that XStream is gona tackle it. or is there any better aproach?
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
Why do you need to send the changed data from the database (one server to another)?


And I'm not sure why you need to put the data into XML?


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Em Aiy
Ranch Hand

Joined: May 11, 2006
Posts: 226
I want to update the synchronize the DB1 with DB2. 2DB's are entirely different and different schema hosted on different locations and i need to sync the information on 2 DB's accordingly.

Why i am putting data in XML is because i need to send the changed information about EACH table to other side and for this i found XML the best thing. I have explained the detail above.
Em Aiy
Ranch Hand

Joined: May 11, 2006
Posts: 226
I am still not able to find a appropriate solution
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generating Dynamic XML using Reflection for Classes