package com.asman;
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;
}
}
package com.asman;
import java.beans.XMLEncoder;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
public class BeanToXMLExp {
public static void main(String[] args) throws Exception {
JAXBContext contextObj = JAXBContext.newInstance(Student.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);
//our xml file store d: directory
marshallerObj .marshal(myStudent, new FileOutputStream("d:\\Student.xml"));
}
}
output:
--------
student.xml
-----------
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<student>
<age>20</age>
<gender>M</gender>
<name>Amar</name>
</student>
No comments:
Post a Comment