File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes XSD and java object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XSD and java object" Watch "XSD and java object" New topic

XSD and java object

pranay hira

Joined: Jun 26, 2006
Posts: 15
Dear All,

When I have a java object say Person and I have an XSD can I generate an output XML file. I heard a collega saying something about SAX and DOM parsers.
But reading through the API I only see the other way around. Going from XML to a Java object.

Can anyone give me a tip?

Thanks in advance
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
You have lots of options.

Since XML is just text, you could create output with println type character output statements. It would be up to you to write text compatible with the XSD.

For objects compatible with the JavaBean standard there is the java.beans.XMLEncoder class

Don't expect some magic tool that can look at the XSD and create complete code.

David Newton

Joined: Sep 29, 2008
Posts: 12617

Creating unmarshalling code via an XSD is a largely automatic process via most any JAXB implementation.
pranay hira

Joined: Jun 26, 2006
Posts: 15
okay guess my collegue misunderstood. I did found this:
JAXBContext contextObj = JAXBContext.newInstance(Student.class);
Student myStudent = new Student();
Marshaller marshallerObj = contextObj.createMarshaller();
marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

marshallerObj .marshal(myStudent, new FileOutputStream("Student.xml"));
I agree. Here's the link:
subject: XSD and java object
It's not a secret anymore!