JAXB: XML 与 Java之间的映射 (OXM)

前端之家收集整理的这篇文章主要介绍了JAXB: XML 与 Java之间的映射 (OXM)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
一、jaxb是什么
JAXB是Java Architecture for XML Binding的缩写。可以将一个Java对象转变成为XML格式,反之亦然。
我们把对象与关系数据库之间的映射称为ORM,其实也可以把对象与XML之间的映射称为OXM(Object XML Mapping)。原来JAXB是Java EE的一部分,在JDK1.6中,SUN将其放到了Java SE中,这也是SUN的一贯做法。JDK1.6中自带的这个JAXB版本是2.0,比起1.0(JSR 31)来,JAXB2(JSR 222)用JDK5的新特性Annotation来标识要作绑定的类和属性等,这就极大简化了开发的工作量。
二、jaxb应用模式

在JAVA EE 5\6中,jaxb可以很方便的与jax-rs、jax-ws集成,极大的简化了web service接口的开发工作量。

三、需要的JAR包:eclipselink

你需要在eclipse中你的项目中引入支持jaxb的jar包,目前的eclipselink2.5.2 or 2.6.x都支持它,下载page如下

https://www.eclipse.org/eclipselink/downloads/prevIoUs_releases.PHP

或者利用"eclipse marketplace" or "install new software" 去自动安装即可。

而且这个jar可以支持JPA等,不错的工具。


jaxb代码举例
第一步:需要引入eclipselink等支持JAXB的包
第二步:编写java bean;

[java] view plain copy
  1. packagecom.mkyong.core;
  2. importjavax.xml.bind.annotation.XmlAttribute;
  3. importjavax.xml.bind.annotation.XmlElement;
  4. importjavax.xml.bind.annotation.XmlRootElement;
  5. @XmlRootElement
  6. publicclassCustomer{
  7. Stringname;
  8. intage;
  9. intid;
  10. publicStringgetName(){
  11. returnname;
  12. }
  13. @XmlElement
  14. voidsetName(Stringname){
  15. this.name=name;
  16. }
  17. intgetAge(){
  18. returnage;
  19. @XmlElement
  20. voidsetAge(intage){
  21. this.age=age;
  22. intgetId(){
  23. returnid;
  24. @XmlAttribute
  25. voidsetId(intid){
  26. this.id=id;
  27. }

第三步:main方法把java bean转化为xml字符串
copy
    importjava.io.File;
  1. importjavax.xml.bind.JAXBContext;
  2. importjavax.xml.bind.JAXBException;
  3. importjavax.xml.bind.Marshaller;
  4. classJAXBExample{
  5. staticvoidmain(String[]args){
  6. Customercustomer=newCustomer();
  7. customer.setId(100);
  8. customer.setName("mkyong");
  9. customer.setAge(29);
  10. try{
  11. Filefile=newFile("C:\\file.xml");
  12. JAXBContextjaxbContext=JAXBContext.newInstance(Customer.class);
  13. MarshallerjaxbMarshaller=jaxbContext.createMarshaller();
  14. //outputprettyprinted
  15. jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
  16. jaxbMarshaller.marshal(customer,file);
  17. jaxbMarshaller.marshal(customer,System.out);
  18. }catch(JAXBExceptione){
  19. e.printStackTrace();
  20. }

下面是输出
[html] copy
    <?xmlversion="1.0"encoding="UTF-8"standalone="yes"?>
  1. <customerid="100">
  2. @H_181_502@age>29</>
  3. name>mkyongcustomer>


jdk提供了xjc工具可以使xsd自动生成相应的java bean,这大大提高了开发的效率。同时,我们也可以使用trang.jar把xml轻松转化为xsd。下面是使用的举例。

第一步:把数据库表映射为xml

copy
    xmlversion="1.0"encoding="UTF-8"Useru_id="1"u_name="moto"u_email="aaa@XXX.com"
  1. u_mood="今天放假了"u_state="online"u_mobile="12345678901"
  2. u_hometown="山西"u_job="IT软件工程师"u_avatar="w34353453543r53"/>
第二步:使用 trang.jar 转化为xsd文件。在命令行执行:

copy
    java-jarD:\lib\trang.jaruser.xmluser.xsd
下面,是生成的User.java。
copy
    //
  1. //ThisfilewasgeneratedbytheJavaTMArchitectureforXMLBinding(JAXB)ReferenceImplementation,vJAXB2.1.10inJDK6
  2. //See<ahref="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
  3. //Anymodificationstothisfilewillbelostuponrecompilationofthesourceschema.
  4. //Generatedon:2011.11.13at01:26:07����CST
  5. //
  6. packagecom.moto.server.bean;
  7. importjava.math.BigInteger;
  8. importjavax.xml.bind.annotation.XmlAccessType;
  9. importjavax.xml.bind.annotation.XmlAccessorType;
  10. importjavax.xml.bind.annotation.XmlAttribute;
  11. importjavax.xml.bind.annotation.XmlSchemaType;
  12. importjavax.xml.bind.annotation.XmlType;
  13. importjavax.xml.bind.annotation.adapters.CollapsedStringAdapter;
  14. importjavax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
  15. /**
  16. *<p>Javaclassforanonymouscomplextype.
  17. *
  18. *<p>Thefollowingschemafragmentspecifiestheexpectedcontentcontainedwithinthisclass.
  19. *<pre>
  20. *<complexType>
  21. *<complexContent>
  22. *<restrictionbase="{http://www.w3.org/2001/XMLSchema}anyType">
  23. *<attributename="u_avatar"use="required"type="{http://www.w3.org/2001/XMLSchema}NCName"/>
  24. *<attributename="u_email"use="required"type="{http://www.w3.org/2001/XMLSchema}anySimpleType"/>
  25. *<attributename="u_hometown"use="required"type="{http://www.w3.org/2001/XMLSchema}NCName"/>
  26. *<attributename="u_id"use="required"type="{http://www.w3.org/2001/XMLSchema}integer"/>
  27. *<attributename="u_job"use="required"type="{http://www.w3.org/2001/XMLSchema}NCName"/>
  28. *<attributename="u_mobile"use="required"type="{http://www.w3.org/2001/XMLSchema}integer"/>
  29. *<attributename="u_mood"use="required"type="{http://www.w3.org/2001/XMLSchema}NCName"/>
  30. *<attributename="u_name"use="required"type="{http://www.w3.org/2001/XMLSchema}NCName"/>
  31. *<attributename="u_state"use="required"type="{http://www.w3.org/2001/XMLSchema}NCName"/>
  32. *</restriction>
  33. *</complexContent>
  34. *</complexType>
  35. *</pre>
  36. *
  37. */
  38. @XmlAccessorType(XmlAccessType.FIELD)
  39. @XmlType(name="")
  40. @XmlRootElement(name="User")
  41. classUser{
  42. @XmlAttribute(name="u_avatar",required=true)
  43. @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
  44. @XmlSchemaType(name="NCName")
  45. protectedStringuAvatar;
  46. @XmlAttribute(name="u_email",100); background-color:inherit">@XmlSchemaType(name="anySimpleType")
  47. protectedStringuEmail;
  48. @XmlAttribute(name="u_hometown",153); font-weight:bold; background-color:inherit">true)
  49. class)
  50. @XmlSchemaType(name="NCName")
  51. protectedStringuHometown;
  52. @XmlAttribute(name="u_id",153); font-weight:bold; background-color:inherit">protectedBigIntegeruId;
  53. @XmlAttribute(name="u_job",153); font-weight:bold; background-color:inherit">protectedStringuJob;
  54. @XmlAttribute(name="u_mobile",153); font-weight:bold; background-color:inherit">protectedBigIntegeruMobile;
  55. @XmlAttribute(name="u_mood",153); font-weight:bold; background-color:inherit">protectedStringuMood;
  56. @XmlAttribute(name="u_name",153); font-weight:bold; background-color:inherit">protectedStringuName;
  57. @XmlAttribute(name="u_state",153); font-weight:bold; background-color:inherit">protectedStringuState;
  58. *GetsthevalueoftheuAvatarproperty.
  59. *@return
  60. *possibleobjectis
  61. *{@linkString}
  62. */
  63. publicStringgetUAvatar(){
  64. returnuAvatar;
  65. *SetsthevalueoftheuAvatarproperty.
  66. *@paramvalue
  67. *allowedobjectis
  68. voidsetUAvatar(Stringvalue){
  69. this.uAvatar=value;
  70. *GetsthevalueoftheuEmailproperty.
  71. publicStringgetUEmail(){
  72. returnuEmail;
  73. *SetsthevalueoftheuEmailproperty.
  74. voidsetUEmail(Stringvalue){
  75. this.uEmail=value;
  76. *GetsthevalueoftheuHometownproperty.
  77. publicStringgetUHometown(){
  78. returnuHometown;
  79. *SetsthevalueoftheuHometownproperty.
  80. voidsetUHometown(Stringvalue){
  81. this.uHometown=value;
  82. *GetsthevalueoftheuIdproperty.
  83. *{@linkBigInteger}
  84. publicBigIntegergetUId(){
  85. returnuId;
  86. *SetsthevalueoftheuIdproperty.
  87. voidsetUId(BigIntegervalue){
  88. this.uId=value;
  89. *GetsthevalueoftheuJobproperty.
  90. publicStringgetUJob(){
  91. returnuJob;
  92. *SetsthevalueoftheuJobproperty.
  93. voidsetUJob(Stringvalue){
  94. this.uJob=value;
  95. *GetsthevalueoftheuMobileproperty.
  96. publicBigIntegergetUMobile(){
  97. returnuMobile;
  98. *SetsthevalueoftheuMobileproperty.
  99. voidsetUMobile(BigIntegervalue){
  100. this.uMobile=value;
  101. *GetsthevalueoftheuMoodproperty.
  102. publicStringgetUMood(){
  103. returnuMood;
  104. *SetsthevalueoftheuMoodproperty.
  105. voidsetUMood(Stringvalue){
  106. this.uMood=value;
  107. *GetsthevalueoftheuNameproperty.
  108. publicStringgetUName(){
  109. returnuName;
  110. *SetsthevalueoftheuNameproperty.
  111. voidsetUName(Stringvalue){
  112. this.uName=value;
  113. *GetsthevalueoftheuStateproperty.
  114. publicStringgetUState(){
  115. returnuState;
  116. *SetsthevalueoftheuStateproperty.
  117. voidsetUState(Stringvalue){
  118. this.uState=value;
  119. }

另外也可以利用eclipse的jaxb项目自己的菜单功能也可以根据 java class自动生成xsd文件,如下图


猜你在找的XML相关文章