- packageycl.learn.xml.jaxb;
- importjava.io.File;
- importjava.io.FileInputStream;
- importjava.io.FileNotFoundException;
- importjava.io.FileOutputStream;
- importjava.io.InputStream;
- importjava.io.OutputStream;
- importjavax.xml.bind.JAXBContext;
- importjavax.xml.bind.JAXBException;
- importjavax.xml.bind.Marshaller;
- importjavax.xml.bind.Unmarshaller;
- publicclassJAXBUtil<T>{
- @SuppressWarnings("unchecked")
- publicTunmarshal(Class<T>clazz,InputStreamis)throwsJAXBException{
- JAXBContextcontext=JAXBContext.newInstance(clazz);
- Unmarshallerun=context.createUnmarshaller();
- return(T)un.unmarshal(is);
- }
- publicTunmarshal(Class<T>clazz,Filefile)throwsJAXBException,FileNotFoundException{
- returnunmarshal(clazz,newFileInputStream(file));
- }
- publicvoidmarshal(Telement,OutputStreamos)throwsJAXBException{
- JAXBContextjc=JAXBContext.newInstance(element.getClass());
- Marshallerm=jc.createMarshaller();
- m.marshal(element,os);
- }
- publicvoidmarshal(Telement,Fileoutput)throwsFileNotFoundException,JAXBException{
- marshal(element,newFileOutputStream(output));
- }
- }
this is the simple util that packaging the JAXB operator.
then we can use it simplely.
1. we just export simple JODO to xml
- packageycl.learn.xml.jaxb;
- importjavax.xml.bind.annotation.XmlRootElement;
- @XmlRootElement
- publicclassEmployeeDO{
- privateintid;
- privateStringgender;
- privateintage;
- privateStringname;
- privateStringrole;
- privateStringpassword;
- publicStringgetPassword(){
- returnpassword;
- }
- publicvoidsetPassword(Stringpassword){
- this.password=password;
- }
- publicintgetId(){
- returnid;
- }
- publicvoidsetId(intid){
- this.id=id;
- }
- publicintgetAge(){
- returnage;
- }
- publicvoidsetAge(intage){
- this.age=age;
- }
- publicStringgetName(){
- returnname;
- }
- publicvoidsetName(Stringname){
- this.name=name;
- }
- publicStringgetGender(){
- returngender;
- }
- publicvoidsetGender(Stringgender){
- this.gender=gender;
- }
- publicStringgetRole(){
- returnrole;
- }
- publicvoidsetRole(Stringrole){
- this.role=role;
- }
- @Override
- publicStringtoString(){
- return"ID="+id+"NAME="+name+"AGE="+age+"GENDER="
- +gender+"ROLE="+role+"PASSWORD="+password;
- }
- }
we must add @XmlRootElement to point this object is the root object for export.
- packageycl.learn.xml.jaxb;
- importjava.io.File;
- importjava.io.FileNotFoundException;
- importjavax.xml.bind.JAXBException;
- publicclassJAXBUtilTest{
- privatestaticfinalStringFILE_NAME="jaxb-emp-jaxbutil.xml";
- publicstaticvoidmain(String[]args)throwsFileNotFoundException,JAXBException{
- EmployeeDOemp=newEmployeeDO();
- emp.setId(1);
- emp.setAge(25);
- emp.setName("Pankaj");
- emp.setGender("Male");
- emp.setRole("Developer");
- emp.setPassword("sensitive");
- JAXBUtil<EmployeeDO>ju=newJAXBUtil<EmployeeDO>();
- ju.marshal(emp,newFile(FILE_NAME));
- System.out.println("generatorsuccess");
- EmployeeDOempant=ju.unmarshal(EmployeeDO.class,newFile(FILE_NAME));
- System.out.println("readersuccess");
- System.out.println(empant);
- }
- }
we can easily use JAXBUtil to marshal Object to file.
we can also easily use JAXBUtil to unmarchal File to Object.
- <?xmlversion="1.0"encoding="UTF-8"standalone="yes"?>
- <employeeDO>
- <age>25</age>
- <gender>Male</gender>
- <id>1</id>
- <name>Pankaj</name>
- <password>sensitive</password>
- <role>Developer</role>
- </employeeDO>
If you review this code carefully,you will find the default root element name is the Object's name with first letter is lowercase.
the element under the root element is the attribute of the object.
Q1: can i change the Element Name,to generator Xml
- packageycl.learn.xml.jaxb;
- importjavax.xml.bind.annotation.XmlElement;
- importjavax.xml.bind.annotation.XmlRootElement;
- @XmlRootElement(name="content")
- publicclassEmployeeDO{
- privateintid;
- privateStringgender;
- privateintage;
- privateStringname;
- privateStringrole;
- privateStringpassword;
- @XmlElement(name="pwd")
- publicStringgetPassword(){
- returnpassword;
- }
- publicvoidsetPassword(Stringpassword){
- this.password=password;
- }
- publicintgetId(){
- returnid;
- }
- publicvoidsetId(intid){
- this.id=id;
- }
- publicintgetAge(){
- returnage;
- }
- publicvoidsetAge(intage){
- this.age=age;
- }
- publicStringgetName(){
- returnname;
- }
- publicvoidsetName(Stringname){
- this.name=name;
- }
- publicStringgetGender(){
- returngender;
- }
- publicvoidsetGender(Stringgender){
- this.gender=gender;
- }
- publicStringgetRole(){
- returnrole;
- }
- publicvoidsetRole(Stringrole){
- this.role=role;
- }
- @Override
- publicStringtoString(){
- return"ID="+id+"NAME="+name+"AGE="+age+"GENDER="
- +gender+"ROLE="+role+"PASSWORD="+password;
- }
- }
we just need to set the @XmlRootElement or @XmlElement's name is ok.then the xml content is
- <?xmlversion="1.0"encoding="UTF-8"standalone="yes"?>
- <content>
- <age>25</age>
- <gender>Male</gender>
- <id>1</id>
- <name>Pankaj</name>
- <pwd>sensitive</pwd>
- <role>Developer</role>
- </content>
Q2: can i point that some attribute of Object i don't want to export
- packageycl.learn.xml.jaxb;
- importjavax.xml.bind.annotation.XmlElement;
- importjavax.xml.bind.annotation.XmlRootElement;
- importjavax.xml.bind.annotation.XmlTransient;
- @XmlRootElement(name="content")
- publicclassEmployeeDO{
- privateintid;
- privateStringgender;
- privateintage;
- privateStringname;
- privateStringrole;
- privateStringpassword;
- @XmlElement(name="pwd")
- publicStringgetPassword(){
- returnpassword;
- }
- publicvoidsetPassword(Stringpassword){
- this.password=password;
- }
- @XmlTransient
- publicintgetId(){
- returnid;
- }
- publicvoidsetId(intid){
- this.id=id;
- }
- publicintgetAge(){
- returnage;
- }
- publicvoidsetAge(intage){
- this.age=age;
- }
- publicStringgetName(){
- returnname;
- }
- publicvoidsetName(Stringname){
- this.name=name;
- }
- publicStringgetGender(){
- returngender;
- }
- publicvoidsetGender(Stringgender){
- this.gender=gender;
- }
- publicStringgetRole(){
- returnrole;
- }
- publicvoidsetRole(Stringrole){
- this.role=role;
- }
- @Override
- publicStringtoString(){
- return"ID="+id+"NAME="+name+"AGE="+age+"GENDER="
- +gender+"ROLE="+role+"PASSWORD="+password;
- }
- }
you can add @XmlTransient before the getMethod,then this attribute won't be export.
- <?xmlversion="1.0"encoding="UTF-8"standalone="yes"?>
- <content>
- <age>25</age>
- <gender>Male</gender>
- <name>Pankaj</name>
- <pwd>sensitive</pwd>
- <role>Developer</role>
- </content>
Q3: can i export the attribute of Object as attribute,not Element of xml,and change the attribute name
- @XmlAttribute(name="agefather")
- publicintgetAge(){
- returnage;
- }
very easily to change sub element to attribute.
- <?xmlversion="1.0"encoding="UTF-8"standalone="yes"?>
- <contentagefather="25">
- <gender>Male</gender>
- <name>Pankaj</name>
- <pwd>sensitive</pwd>
- <role>Developer</role>
- </content>
Q4: i want to export the xml element as my order
- @XmlRootElement(name="content")
- @XmlType(propOrder={"gender","age","name","role","password"})
- publicclassEmployeeDO
- <?xmlversion="1.0"encoding="UTF-8"standalone="yes"?>
- <contentagefather="25">
- <gender>Male</gender>
- <name>Pankaj</name>
- <role>Developer</role>
- <pwd>sensitive</pwd>
- </content>
you can see this element is ordered by our defined.
Q5: i want to export list of Objects
- @XmlElementWrapper(name="employees")
- @XmlElement(name="employee")
- publicList<Employee>getEmployees(){
- returnemployees;
- }
- <employees>
- <employee>
- <age>25</age>
- <gender>GGGGGG</gender>
- <id>1</id>
- <name>Pankaj</name>
- <password>sensitive</password>
- <role>Developer</role>
- <family></family>
- </employee>
- <employee>
- <age>25</age>
- <gender>GGGGGGG</gender>
- <id>1</id>
- <name>Pankaj</name>
- <password>sensitive</password>
- <role>Developer</role>
- <family></family>
- </employee>
- </employees>
Q5: i want to export list of Objects in other Object
- [ClassA]
- publicFamilygetFamily(){
- returnfamily;
- }
- publicclassFamily{
- privateList<Employee>employees;
- @XmlElement(name="employee")
- publicList<Employee>getEmployees(){
- returnemployees;
- }
- publicvoidsetEmployees(List<Employee>employees){
- this.employees=employees;
- }
- }
- <family>
- <employee>
- <age>25</age>
- <gender>LLLLLLL</gender>
- <id>1</id>
- <name>Pankaj</name>
- <password>sensitive</password>
- <role>Developer</role>
- <family></family>
- </employee>
- <employee>
- <age>25</age>
- <gender>Male</gender>
- <id>1</id>
- <name>Pankaj</name>
- <password>sensitive</password>
- <role>Developer</role>
- <family></family>
- </employee>
- </family>
oh,you just don't to defined family,because it is the element of the object,default define is ok. the employee is multiple in family,you just define the @XmlElement empoyee is ok,and this will be auto add to list so this is you can think of the four questions,and i can't think other situations,and i think this four points can resolve all problems that we encountered,we can easily change xml Element name,Element attribute,Element order,that's cool.