Xml与Java Object 的转换[JAXB]

前端之家收集整理的这篇文章主要介绍了Xml与Java Object 的转换[JAXB]前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

http://a123159521.iteye.com/blog/1987300

Java代码
  1. packageycl.learn.xml.jaxb;
  2. importjava.io.File;
  3. importjava.io.FileInputStream;
  4. importjava.io.FileNotFoundException;
  5. importjava.io.FileOutputStream;
  6. importjava.io.InputStream;
  7. importjava.io.OutputStream;
  8. importjavax.xml.bind.JAXBContext;
  9. importjavax.xml.bind.JAXBException;
  10. importjavax.xml.bind.Marshaller;
  11. importjavax.xml.bind.Unmarshaller;
  12. publicclassJAXBUtil<T>{
  13. @SuppressWarnings("unchecked")
  14. publicTunmarshal(Class<T>clazz,InputStreamis)throwsJAXBException{
  15. JAXBContextcontext=JAXBContext.newInstance(clazz);
  16. Unmarshallerun=context.createUnmarshaller();
  17. return(T)un.unmarshal(is);
  18. }
  19. publicTunmarshal(Class<T>clazz,Filefile)throwsJAXBException,FileNotFoundException{
  20. returnunmarshal(clazz,newFileInputStream(file));
  21. }
  22. publicvoidmarshal(Telement,OutputStreamos)throwsJAXBException{
  23. JAXBContextjc=JAXBContext.newInstance(element.getClass());
  24. Marshallerm=jc.createMarshaller();
  25. m.marshal(element,os);
  26. }
  27. publicvoidmarshal(Telement,Fileoutput)throwsFileNotFoundException,JAXBException{
  28. marshal(element,newFileOutputStream(output));
  29. }
  30. }


this is the simple util that packaging the JAXB operator.
then we can use it simplely.

1. we just export simple JODO to xml
Java代码
  1. packageycl.learn.xml.jaxb;
  2. importjavax.xml.bind.annotation.XmlRootElement;
  3. @XmlRootElement
  4. publicclassEmployeeDO{
  5. privateintid;
  6. privateStringgender;
  7. privateintage;
  8. privateStringname;
  9. privateStringrole;
  10. privateStringpassword;
  11. publicStringgetPassword(){
  12. returnpassword;
  13. }
  14. publicvoidsetPassword(Stringpassword){
  15. this.password=password;
  16. }
  17. publicintgetId(){
  18. returnid;
  19. }
  20. publicvoidsetId(intid){
  21. this.id=id;
  22. }
  23. publicintgetAge(){
  24. returnage;
  25. }
  26. publicvoidsetAge(intage){
  27. this.age=age;
  28. }
  29. publicStringgetName(){
  30. returnname;
  31. }
  32. publicvoidsetName(Stringname){
  33. this.name=name;
  34. }
  35. publicStringgetGender(){
  36. returngender;
  37. }
  38. publicvoidsetGender(Stringgender){
  39. this.gender=gender;
  40. }
  41. publicStringgetRole(){
  42. returnrole;
  43. }
  44. publicvoidsetRole(Stringrole){
  45. this.role=role;
  46. }
  47. @Override
  48. publicStringtoString(){
  49. return"ID="+id+"NAME="+name+"AGE="+age+"GENDER="
  50. +gender+"ROLE="+role+"PASSWORD="+password;
  51. }
  52. }


we must add @XmlRootElement to point this object is the root object for export.
Java代码
  1. packageycl.learn.xml.jaxb;
  2. importjava.io.File;
  3. importjava.io.FileNotFoundException;
  4. importjavax.xml.bind.JAXBException;
  5. publicclassJAXBUtilTest{
  6. privatestaticfinalStringFILE_NAME="jaxb-emp-jaxbutil.xml";
  7. /**
  8. *@paramargs
  9. *@throwsJAXBException
  10. *@throwsFileNotFoundException
  11. */
  12. publicstaticvoidmain(String[]args)throwsFileNotFoundException,JAXBException{
  13. //TODOAuto-generatedmethodstub
  14. EmployeeDOemp=newEmployeeDO();
  15. emp.setId(1);
  16. emp.setAge(25);
  17. emp.setName("Pankaj");
  18. emp.setGender("Male");
  19. emp.setRole("Developer");
  20. emp.setPassword("sensitive");
  21. JAXBUtil<EmployeeDO>ju=newJAXBUtil<EmployeeDO>();
  22. ju.marshal(emp,newFile(FILE_NAME));
  23. System.out.println("generatorsuccess");
  24. EmployeeDOempant=ju.unmarshal(EmployeeDO.class,newFile(FILE_NAME));
  25. System.out.println("readersuccess");
  26. System.out.println(empant);
  27. }
  28. }


we can easily use JAXBUtil to marshal Object to file.
we can also easily use JAXBUtil to unmarchal File to Object.

Java代码
  1. <?xmlversion="1.0"encoding="UTF-8"standalone="yes"?>
  2. <employeeDO>
  3. <age>25</age>
  4. <gender>Male</gender>
  5. <id>1</id>
  6. <name>Pankaj</name>
  7. <password>sensitive</password>
  8. <role>Developer</role>
  9. </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
Java代码
  1. packageycl.learn.xml.jaxb;
  2. importjavax.xml.bind.annotation.XmlElement;
  3. importjavax.xml.bind.annotation.XmlRootElement;
  4. @XmlRootElement(name="content")
  5. publicclassEmployeeDO{
  6. privateintid;
  7. privateStringgender;
  8. privateintage;
  9. privateStringname;
  10. privateStringrole;
  11. privateStringpassword;
  12. @XmlElement(name="pwd")
  13. publicStringgetPassword(){
  14. returnpassword;
  15. }
  16. publicvoidsetPassword(Stringpassword){
  17. this.password=password;
  18. }
  19. publicintgetId(){
  20. returnid;
  21. }
  22. publicvoidsetId(intid){
  23. this.id=id;
  24. }
  25. publicintgetAge(){
  26. returnage;
  27. }
  28. publicvoidsetAge(intage){
  29. this.age=age;
  30. }
  31. publicStringgetName(){
  32. returnname;
  33. }
  34. publicvoidsetName(Stringname){
  35. this.name=name;
  36. }
  37. publicStringgetGender(){
  38. returngender;
  39. }
  40. publicvoidsetGender(Stringgender){
  41. this.gender=gender;
  42. }
  43. publicStringgetRole(){
  44. returnrole;
  45. }
  46. publicvoidsetRole(Stringrole){
  47. this.role=role;
  48. }
  49. @Override
  50. publicStringtoString(){
  51. return"ID="+id+"NAME="+name+"AGE="+age+"GENDER="
  52. +gender+"ROLE="+role+"PASSWORD="+password;
  53. }
  54. }

we just need to set the @XmlRootElement or @XmlElement's name is ok.then the xml content is
Java代码
  1. <?xmlversion="1.0"encoding="UTF-8"standalone="yes"?>
  2. <content>
  3. <age>25</age>
  4. <gender>Male</gender>
  5. <id>1</id>
  6. <name>Pankaj</name>
  7. <pwd>sensitive</pwd>
  8. <role>Developer</role>
  9. </content>


Q2: can i point that some attribute of Object i don't want to export

Java代码
  1. packageycl.learn.xml.jaxb;
  2. importjavax.xml.bind.annotation.XmlElement;
  3. importjavax.xml.bind.annotation.XmlRootElement;
  4. importjavax.xml.bind.annotation.XmlTransient;
  5. @XmlRootElement(name="content")
  6. publicclassEmployeeDO{
  7. privateintid;
  8. privateStringgender;
  9. privateintage;
  10. privateStringname;
  11. privateStringrole;
  12. privateStringpassword;
  13. @XmlElement(name="pwd")
  14. publicStringgetPassword(){
  15. returnpassword;
  16. }
  17. publicvoidsetPassword(Stringpassword){
  18. this.password=password;
  19. }
  20. @XmlTransient
  21. publicintgetId(){
  22. returnid;
  23. }
  24. publicvoidsetId(intid){
  25. this.id=id;
  26. }
  27. publicintgetAge(){
  28. returnage;
  29. }
  30. publicvoidsetAge(intage){
  31. this.age=age;
  32. }
  33. publicStringgetName(){
  34. returnname;
  35. }
  36. publicvoidsetName(Stringname){
  37. this.name=name;
  38. }
  39. publicStringgetGender(){
  40. returngender;
  41. }
  42. publicvoidsetGender(Stringgender){
  43. this.gender=gender;
  44. }
  45. publicStringgetRole(){
  46. returnrole;
  47. }
  48. publicvoidsetRole(Stringrole){
  49. this.role=role;
  50. }
  51. @Override
  52. publicStringtoString(){
  53. return"ID="+id+"NAME="+name+"AGE="+age+"GENDER="
  54. +gender+"ROLE="+role+"PASSWORD="+password;
  55. }
  56. }


you can add @XmlTransient before the getMethod,then this attribute won't be export.
Java代码
  1. <?xmlversion="1.0"encoding="UTF-8"standalone="yes"?>
  2. <content>
  3. <age>25</age>
  4. <gender>Male</gender>
  5. <name>Pankaj</name>
  6. <pwd>sensitive</pwd>
  7. <role>Developer</role>
  8. </content>


Q3: can i export the attribute of Object as attribute,not Element of xml,and change the attribute name

Java代码
  1. @XmlAttribute(name="agefather")
  2. publicintgetAge(){
  3. returnage;
  4. }

very easily to change sub element to attribute.

Java代码
  1. <?xmlversion="1.0"encoding="UTF-8"standalone="yes"?>
  2. <contentagefather="25">
  3. <gender>Male</gender>
  4. <name>Pankaj</name>
  5. <pwd>sensitive</pwd>
  6. <role>Developer</role>
  7. </content>


Q4: i want to export the xml element as my order

Java代码
  1. @XmlRootElement(name="content")
  2. @XmlType(propOrder={"gender","age","name","role","password"})
  3. publicclassEmployeeDO


Java代码
  1. <?xmlversion="1.0"encoding="UTF-8"standalone="yes"?>
  2. <contentagefather="25">
  3. <gender>Male</gender>
  4. <name>Pankaj</name>
  5. <role>Developer</role>
  6. <pwd>sensitive</pwd>
  7. </content>


you can see this element is ordered by our defined.

Q5: i want to export list of Objects

Java代码
  1. @XmlElementWrapper(name="employees")
  2. @XmlElement(name="employee")
  3. publicList<Employee>getEmployees(){
  4. returnemployees;
  5. }


Java代码
  1. <employees>
  2. <employee>
  3. <age>25</age>
  4. <gender>GGGGGG</gender>
  5. <id>1</id>
  6. <name>Pankaj</name>
  7. <password>sensitive</password>
  8. <role>Developer</role>
  9. <family></family>
  10. </employee>
  11. <employee>
  12. <age>25</age>
  13. <gender>GGGGGGG</gender>
  14. <id>1</id>
  15. <name>Pankaj</name>
  16. <password>sensitive</password>
  17. <role>Developer</role>
  18. <family></family>
  19. </employee>
  20. </employees>


Q5: i want to export list of Objects in other Object

Java代码
  1. [ClassA]
  2. publicFamilygetFamily(){
  3. returnfamily;
  4. }


Java代码
  1. publicclassFamily{
  2. privateList<Employee>employees;
  3. @XmlElement(name="employee")
  4. publicList<Employee>getEmployees(){
  5. returnemployees;
  6. }
  7. publicvoidsetEmployees(List<Employee>employees){
  8. this.employees=employees;
  9. }
  10. }

Java代码
  1. <family>
  2. <employee>
  3. <age>25</age>
  4. <gender>LLLLLLL</gender>
  5. <id>1</id>
  6. <name>Pankaj</name>
  7. <password>sensitive</password>
  8. <role>Developer</role>
  9. <family></family>
  10. </employee>
  11. <employee>
  12. <age>25</age>
  13. <gender>Male</gender>
  14. <id>1</id>
  15. <name>Pankaj</name>
  16. <password>sensitive</password>
  17. <role>Developer</role>
  18. <family></family>
  19. </employee>
  20. </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.

猜你在找的XML相关文章