• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

XSD and java object

 
pranay hira
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Creating unmarshalling code via an XSD is a largely automatic process via most any JAXB implementation.
 
pranay hira
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"));
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic