aspose file tools*
The moose likes XML and Related Technologies and the fly likes converting xml to java objects 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 » Engineering » XML and Related Technologies
Bookmark "converting xml to java objects" Watch "converting xml to java objects" New topic
Author

converting xml to java objects

Christopher Au
Greenhorn

Joined: Jul 26, 2007
Posts: 21
I would like to know whether it is possible to take a xml file and then get an java object equivalent. I have been able to use castor to generate classes from a schema generated from an xml file, however from what I can gather we cannot use those generated classes until they are compiled and placed into the classpath. If what I am asking is not possible I would like to know is how what alternatives do I have.

Thanks

Chris
dennis zined
Ranch Hand

Joined: Mar 07, 2003
Posts: 330
you may want to take a look at xmlbeans.


SCJP 1.4<br />SCWCD 1.4
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
If you are saying that you need something that would give you objects straight away from xml , then its not possible.
You have to have class first before creating objects out of it.After all class is the blueprint from which you create object.

However you might automate the process to make java class using castor and then compiling those into classes and then jar those classes and using that in your application.


Rahul Bhattacharjee
LinkedIn - Blog
Ugender Rekulampally
Ranch Hand

Joined: Nov 14, 2005
Posts: 130
Chris,

Here is some link which explains how to generate java objects from an XSD.
http://www.onjava.com/pub/a/onjava/2004/12/15/jaxb.html

This sample actually shows how to write data elements into the generated java objects and then producing XML document from those objects. So this part is called Marshalling. and I know you are asking about 'unmarshalling' and I couldnt find a better sample example on the web to give you a link but I did work on this part and I may be able to help you with that...

first get familiarize with the xjc stuff from above link (generating java objects from the schema) and see the above example (marshalling) and unmarshalling will be very simple. just other way around to marshalling example.


here is some sample code I wrote ..





in the above code, "generatedLogResultInputValues" is the directory of the java classes which are generated by XSD.

and "logXMLstr" is the XML string that I am trying to convert into the Java objects...

Hope that helps. And let me know if I can help further.


Thanks,
Ugender
[ September 18, 2007: Message edited by: Ugender Rekulampally ]
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Originally posted by Ugender Rekulampally:
Chris,

Here is some link which explains how to generate java objects from an XSD.
http://www.onjava.com/pub/a/onjava/2004/12/15/jaxb.html

This sample actually shows how to write data elements into the generated java objects and then producing XML document from those objects. So this part is called Marshalling. and I know you are asking about 'unmarshalling' and I couldnt find a better sample example on the web to give you a link but I did work on this part and I may be able to help you with that...

first get familiarize with the xjc stuff from above link (generating java objects from the schema) and see the above example (marshalling) and unmarshalling will be very simple. just other way around to marshalling example.


here is some sample code I wrote ..





in the above code, "generatedLogResultInputValues" is the directory of the java classes which are generated by XSD.

and "logXMLstr" is the XML string that I am trying to convert into the Java objects...

Hope that helps. And let me know if I can help further.


Thanks,
Ugender

[ September 18, 2007: Message edited by: Ugender Rekulampally ]


I guess Christopher is able to do all this and has asked for something else.
Ugender Rekulampally
Ranch Hand

Joined: Nov 14, 2005
Posts: 130
OK. I thought he was looking for some alternatives..

If what I am asking is not possible I would like to know is how what alternatives do I have.

I actually did all that work what Chris was doing using just plain JAXB instead of Castor...anyways, it might be helpful to some one else who will just look for unmarshalling in Jaxb then.


Thanks,
Ugender
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
May be you are correct.But reading the post made me feel that the original poster is aware of the marshalling stuff and wants to map an XML to objects without taking the pain to create objects out of classes and thats not possible.
Let the original poster clarify this. ;)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: converting xml to java objects