• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

convert javaobjects to xml and xml to java objects dynamically

 
kiran manohar
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
kiran manohar
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
after parsing the xml file how to use the Document object to initialize my object.thanks for ur help Lasse.
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JAXB generates objects for a specific schema , so they are not generic.
 
Phil Chuang
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks! I had somehow forgotten Skaringa completely.
 
kiran manohar
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic