This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generating Dynamic XML using Reflection for Classes

 
Em Aiy
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15492
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Em Aiy
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out XStream.
 
Em Aiy
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Em Aiy
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am still not able to find a appropriate solution
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic