aspose file tools*
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


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XSD and java object" Watch "XSD and java object" New topic
Author

XSD and java object

pranay hira
Greenhorn

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
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
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.

Bill
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

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);

myStudent.setGender("M");
myStudent.setName("Amar");
myStudent.setAge(20);
marshallerObj .marshal(myStudent, new FileOutputStream("Student.xml"));
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: XSD and java object