| Author |
convert javaobjects to xml and xml to java objects dynamically
|
kiran manohar
Ranch Hand
Joined: Sep 28, 2003
Posts: 49
|
|
hi, i have a class called Converter which needs to do 4 things. 1) It will receive a xml file .this xml file i need to convert to a object with the datas in the xml stored in the object. for ex. if i get a xml file like say foo.xml[this xml file will change] <mail> <to>raj@yahoo.com</to> <from>kishore@hotmail.com</from> <subject>Hello Raj</subject> <message>Have a nice time</message> </mail> now this xml files data between the markups must be initialized in a java object. and that object which is initialized must be returned by my class. 2) i will receive a object which has been initialized with data . now i need to read the data in the object and write them to a xml file. 3) i will receive a xml string representing some content in it to my class , this xml string needs to be read and its data need to be written to an object.[note: we can assume the content to be the data between the tags in the above xml file] 4) i will receive an object , to which my class Converter must read its content and return a xml string with the objects content.this xml string i need to return from my class. My question is what xml parser should i use , jaxb or castor have given examples which are static in the sense that they advocate getter and setter methods which are based on the xml file. but the xml file i get could contain any structure, so i need a parser which would fix my problem dynamically. in future i need to parse a xml schema file also .so pl take that into consideration and recommend a xml parser which will convert xml file / schema to java objects and vice versa.this looks tough for me as i am new to databinding and xml.Any recommendations will be helpful. Thanks in advance. Kiranmanohar
|
 |
Lasse Koskela
author
Sheriff
Joined: Jan 23, 2002
Posts: 11962
|
|
|
How about just reading the XML into a DOM tree and implementing the conversion manually? I haven't had much experience with binding products such as JAXB or Castor so I cannot say whether they would be helpful in this kind of generic binding.
|
Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
|
 |
kiran manohar
Ranch Hand
Joined: Sep 28, 2003
Posts: 49
|
|
i am new to xml& java databinding , u had mentioned about reading the xml in a dom tree could u give me few examples to get me started. Thanks in advance Kiranmanohar
|
 |
Lasse Koskela
author
Sheriff
Joined: Jan 23, 2002
Posts: 11962
|
|
|
Here's a good example of how to use the DocumentBuilder utility for parsing an XML file into an org.w3c.dom.Document object.
|
 |
kiran manohar
Ranch Hand
Joined: Sep 28, 2003
Posts: 49
|
|
|
after parsing the xml file how to use the Document object to initialize my object.thanks for ur help Lasse.
|
 |
Lasse Koskela
author
Sheriff
Joined: Jan 23, 2002
Posts: 11962
|
|
You traverse through the DOM tree, pick element values where appropriate, and call setThisAndThat("...") on your domain object. Here's a very simple example:
|
 |
kiran manohar
Ranch Hand
Joined: Sep 28, 2003
Posts: 49
|
|
Thanks Lasse, Ur coding is working. But the coding is not generic i presume. that is if i want to send any xml file and parse it to java objects how to go about it Regards Kiranmanohar
|
 |
kiran manohar
Ranch Hand
Joined: Sep 28, 2003
Posts: 49
|
|
lasse, i have a doubt . u said that DOM is to be used in the coding. my doubt is would it parse an xml schema as well as a xml file and also a xml string. that is a string object holding xml tags in it. Thanks Regards Kiranmanohar
|
 |
Lasse Koskela
author
Sheriff
Joined: Jan 23, 2002
Posts: 11962
|
|
that is if i want to send any xml file and parse it to java objects how to go about it
Either you need to implement your own Java-to-XML mapping or use an existing XML data binding API such as Sun's JAXB or Castor.
u said that DOM is to be used in the coding. my doubt is would it parse an xml schema as well as a xml file and also a xml string. that is a string object holding xml tags in it.
Could you rephrase this 'cause I didn't quite get what you're asking?
|
 |
Lasse Koskela
author
Sheriff
Joined: Jan 23, 2002
Posts: 11962
|
|
|
By the way, the DOM tree generated by a DOM parser is as generic a Java object representation of your arbitrary XML document as possible...
|
 |
Pradeep bhatt
Ranch Hand
Joined: Feb 27, 2002
Posts: 8876
|
|
|
JAXB generates objects for a specific schema , so they are not generic.
|
Groovy
|
 |
Phil Chuang
Ranch Hand
Joined: Feb 15, 2003
Posts: 251
|
|
There's an open source package called skaringa that does objects -> xml -> objects that does what you want. http://skaringa.sourceforge.net/ I built myself a handy little wrapper around it and I use it like
|
 |
Lasse Koskela
author
Sheriff
Joined: Jan 23, 2002
Posts: 11962
|
|
Thanks! I had somehow forgotten Skaringa completely.
|
 |
kiran manohar
Ranch Hand
Joined: Sep 28, 2003
Posts: 49
|
|
thanks lasse for the skaringa links, but for now i will continue with DOM.in the coding for dom u have used Agent . in which package does this Agent Class or Interface is used and how to add it. i m using jdk1.4 by the way. Regards Kiranmanohar
|
 |
 |
|
|
subject: convert javaobjects to xml and xml to java objects dynamically
|
|
|