Tue Jul 07, 2009 11:17 pm
import java.io.FileOutputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
public class ObjectToXml {
public static void main(String[] args) throws Exception {
JAXBContext contextObj = JAXBContext.newInstance(Employee.class);
Marshaller marshallerObj = contextObj.createMarshaller();
marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
Student myStudent = new Student();
myStudent.setGender("M");
myStudent.setName("Amar");
myStudent.setAge(20);
marshallerObj .marshal(myStudent, System.out);
}
}
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
class Student{
private String gender;
private String name;
private int age;
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender= gender;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age= age;
}
}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<student>
<gender>M</gender>
<name>Amar</name>
<age>20</age>
</student>
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.