java 关于xml的注解,自动生成xml文件 – @XML

  • Post author:
  • Post category:java



用的是jdk自带的javax.xml.bind.JAXBContext将对象和xml字符串进行相互转换。

如果对要生成的 xml 格式有点些许的限制,就会对生成xml的对象就需要进行些许控制,控制对象的一个最可行的办法就是用注解。
比较常用的几个:
@XmlRootElement:根节点
@XmlAttribute:该属性作为xml的attribute
@XmlElement:该属性作为xml的element,且可以增加属性(name=”NewElementName”),那么生成的xml串的elment的标签是NewElementName
示例:


  1. package


    test;




  2. import


    java.io.StringWriter;




  3. import


    javax.xml.bind.JAXBContext;



  4. import


    javax.xml.bind.JAXBException;



  5. import


    javax.xml.bind.Marshaller;




  6. import


    test.bean.EleClassA;



  7. import


    test.bean.EleClassB;



  8. import


    test.bean.RootClass;




  9. public




    class


    Test1 {




  10. public




    static




    void


    main(String[] args) {


  11. RootClass rc =

    new


    RootClass();


  12. EleClassA a =

    new


    EleClassA();


  13. EleClassB b =

    new


    EleClassB();



  14. a.setAttrC(

    “attrc”


    );


  15. a.setEleA(

    “eleA”


    );


  16. a.setEleB(

    “eleB”


    );



  17. b.setAttrPassword(

    “attrPassword”


    );


  18. b.setAttrUserName(

    “attrUsrName”


    );


  19. b.setEleCode(

    “eleCode”


    );



  20. rc.setA(a);

  21. rc.setB(b);

  22. rc.setRoot(

    “root”


    );


  23. rc.setRootA(

    “rootA”


    );




  24. JAXBContext context;


  25. try


    {


  26. context = JAXBContext.newInstance(RootClass.

    class


    );


  27. Marshaller mar = context.createMarshaller();

  28. mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,

    true


    );


  29. mar.setProperty(Marshaller.JAXB_ENCODING,

    “UTF-8”


    );



  30. StringWriter writer =

    new


    StringWriter();



  31. mar.marshal(rc, writer);


  32. System.out.println(writer.toString());

  33. }

    catch


    (JAXBException e) {


  34. e.printStackTrace();

  35. }


  36. }

  37. }




  38. package


    test.bean;




  39. import


    javax.xml.bind.annotation.XmlElement;



  40. import


    javax.xml.bind.annotation.XmlRootElement;




  41. @XmlRootElement


    (name=


    “rootclass”


    )



  42. public




    class


    RootClass {




  43. private


    EleClassA a;



  44. private


    EleClassB b;



  45. private


    String root;



  46. private


    String rootA;




  47. @XmlElement


    (name=


    “eleClassA”


    )



  48. public


    EleClassA getA() {



  49. return


    a;


  50. }


  51. public




    void


    setA(EleClassA a) {



  52. this


    .a = a;


  53. }


  54. @XmlElement


    (name=


    “EleclassA”


    )



  55. public


    EleClassB getB() {



  56. return


    b;


  57. }


  58. public




    void


    setB(EleClassB b) {



  59. this


    .b = b;


  60. }


  61. public


    String getRoot() {



  62. return


    root;


  63. }


  64. public




    void


    setRoot(String root) {



  65. this


    .root = root;


  66. }


  67. public


    String getRootA() {



  68. return


    rootA;


  69. }


  70. public




    void


    setRootA(String rootA) {



  71. this


    .rootA = rootA;


  72. }


  73. }




  74. package


    test.bean;




  75. import


    javax.xml.bind.annotation.XmlAttribute;



  76. import


    javax.xml.bind.annotation.XmlElement;




  77. public




    class


    EleClassA {




  78. private


    String eleA;



  79. private


    String eleB;




  80. private


    String attrC;




  81. @XmlElement





  82. public


    String getEleA() {



  83. return


    eleA;


  84. }



  85. public




    void


    setEleA(String eleA) {



  86. this


    .eleA = eleA;


  87. }



  88. @XmlElement


    (name=


    “elebnewname”


    )



  89. public


    String getEleB() {



  90. return


    eleB;


  91. }



  92. public




    void


    setEleB(String eleB) {



  93. this


    .eleB = eleB;


  94. }



  95. @XmlAttribute


    ()



  96. public


    String getAttrC() {



  97. return


    attrC;


  98. }



  99. public




    void


    setAttrC(String attrC) {



  100. this


    .attrC = attrC;


  101. }


  102. }




  103. package


    test.bean;




  104. import


    javax.xml.bind.annotation.XmlAttribute;



  105. import


    javax.xml.bind.annotation.XmlElement;




  106. public




    class


    EleClassB {




  107. private


    String attrUserName;



  108. private


    String attrPassword;




  109. private


    String eleCode;




  110. @XmlAttribute





  111. public


    String getAttrUserName() {



  112. return


    attrUserName;


  113. }


  114. public




    void


    setAttrUserName(String attrUserName) {



  115. this


    .attrUserName = attrUserName;


  116. }


  117. @XmlAttribute


    (name=


    “password”


    )



  118. public


    String getAttrPassword() {



  119. return


    attrPassword;


  120. }


  121. public




    void


    setAttrPassword(String attrPassword) {



  122. this


    .attrPassword = attrPassword;


  123. }


  124. @XmlElement





  125. public


    String getEleCode() {



  126. return


    eleCode;


  127. }


  128. public




    void


    setEleCode(String eleCode) {



  129. this


    .eleCode = eleCode;


  130. }



  131. }

运行Test1类中main方法,执行结果:



  1. <?


    xml




    version


    =


    “1.0”




    encoding


    =


    “UTF-8”




    standalone


    =


    “yes”


    ?>





  2. <


    rootclass


    >





  3. <


    eleClassA




    attrC


    =


    “attrc”


    >





  4. <


    eleA


    >


    eleA


    </


    eleA


    >





  5. <


    elebnewname


    >


    eleB


    </


    elebnewname


    >





  6. </


    eleClassA


    >





  7. <


    EleclassA




    attrUserName


    =


    “attrUsrName”




    password


    =


    “attrPassword”


    >





  8. <


    eleCode


    >


    eleCode


    </


    eleCode


    >





  9. </


    EleclassA


    >





  10. <


    root


    >


    root


    </


    root


    >





  11. <


    rootA


    >


    rootA


    </


    rootA


    >





  12. </


    rootclass


    >





版权声明:本文为liushengbaba原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。