一、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 56中,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;
- packagecom.mkyong.core;
-
- importjavax.xml.bind.annotation.XmlAttribute;
- importjavax.xml.bind.annotation.XmlElement;
- importjavax.xml.bind.annotation.XmlRootElement;
- @XmlRootElement
- publicclassCustomer{
-
- Stringname;
- intage;
- intid;
- publicStringgetName(){
- returnname;
- }
- @XmlElement
- voidsetName(Stringname){
- this.name=name;
- }
- intgetAge(){
- returnage;
- @XmlElement
- voidsetAge(intage){
- this.age=age;
- intgetId(){
- returnid;
- @XmlAttribute
- voidsetId(intid){
- this.id=id;
- }
第三步:main方法把java bean转化为xml字符串
importjava.io.File;
- importjavax.xml.bind.JAXBContext;
- importjavax.xml.bind.JAXBException;
- importjavax.xml.bind.Marshaller;
- classJAXBExample{
- staticvoidmain(String[]args){
- Customercustomer=newCustomer();
- customer.setId(100);
- customer.setName("mkyong");
- customer.setAge(29);
- try{
- Filefile=newFile("C:file.xml");
- JAXBContextjaxbContext=JAXBContext.newInstance(Customer.class);
- MarshallerjaxbMarshaller=jaxbContext.createMarshaller();
-
- jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
- jaxbMarshaller.marshal(customer,file);
- jaxbMarshaller.marshal(customer,System.out);
- }catch(JAXBExceptione){
- e.printStackTrace();
- }
下面是输出:
<?xmlversion="1.0"encoding="UTF-8"standalone="yes"?>
- <customerid="100">
- age>29</>
- name>mkyongcustomer>
jdk提供了xjc工具可以使xsd自动生成相应的java bean,这大大提高了开发的效率。同时,我们也可以使用trang.jar把xml轻松转化为xsd。下面是使用的举例。
第一步:把数据库表映射为xml
xmlversion="1.0"encoding="UTF-8"Useru_id="1"u_name="moto"u_email="aaa@XXX.com"
- u_mood="今天放假了"u_state="online"u_mobile="12345678901"
- u_hometown="山西"u_job="IT软件工程师"u_avatar="w34353453543r53"/>
第二步:使用
trang.jar
转化为xsd文件。在命令行执行:
java-jarD:libtrang.jaruser.xmluser.xsd
下面,是生成的User.java。
- //ThisfilewasgeneratedbytheJavaTMArchitectureforXMLBinding(JAXB)ReferenceImplementation,vJAXB2.1.10inJDK6
- //See<ahref="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
- //Anymodificationstothisfilewillbelostuponrecompilationofthesourceschema.
- //Generatedon:2011.11.13at01:26:07????CST
- //
- packagecom.moto.server.bean;
- importjava.math.BigInteger;
- importjavax.xml.bind.annotation.XmlAccessType;
- importjavax.xml.bind.annotation.XmlAccessorType;
- importjavax.xml.bind.annotation.XmlAttribute;
- importjavax.xml.bind.annotation.XmlSchemaType;
- importjavax.xml.bind.annotation.XmlType;
- importjavax.xml.bind.annotation.adapters.CollapsedStringAdapter;
- importjavax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
- /**
- *<p>Javaclassforanonymouscomplextype.
- *
- *<p>Thefollowingschemafragmentspecifiestheexpectedcontentcontainedwithinthisclass.
- *<pre>
- *<complexType>
- *<complexContent>
- *<restrictionbase="{http://www.w3.org/2001/XMLSchema}anyType">
- *<attributename="u_avatar"use="required"type="{http://www.w3.org/2001/XMLSchema}NCName"/>
- *<attributename="u_email"use="required"type="{http://www.w3.org/2001/XMLSchema}anySimpleType"/>
- *<attributename="u_hometown"use="required"type="{http://www.w3.org/2001/XMLSchema}NCName"/>
- *<attributename="u_id"use="required"type="{http://www.w3.org/2001/XMLSchema}integer"/>
- *<attributename="u_job"use="required"type="{http://www.w3.org/2001/XMLSchema}NCName"/>
- *<attributename="u_mobile"use="required"type="{http://www.w3.org/2001/XMLSchema}integer"/>
- *<attributename="u_mood"use="required"type="{http://www.w3.org/2001/XMLSchema}NCName"/>
- *<attributename="u_name"use="required"type="{http://www.w3.org/2001/XMLSchema}NCName"/>
- *<attributename="u_state"use="required"type="{http://www.w3.org/2001/XMLSchema}NCName"/>
- *</restriction>
- *</complexContent>
- *</complexType>
- *</pre>
- *
- */
- @XmlAccessorType(XmlAccessType.FIELD)
- @XmlType(name="")
- @XmlRootElement(name="User")
- classUser{
- @XmlAttribute(name="u_avatar",required=true)
- @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
- @XmlSchemaType(name="NCName")
- protectedStringuAvatar;
- @XmlAttribute(name="u_email",100); background-color:inherit">@XmlSchemaType(name="anySimpleType")
- protectedStringuEmail;
- @XmlAttribute(name="u_hometown",153); font-weight:bold; background-color:inherit">true)
- class)
- @XmlSchemaType(name="NCName")
- protectedStringuHometown;
- @XmlAttribute(name="u_id",153); font-weight:bold; background-color:inherit">protectedBigIntegeruId;
- @XmlAttribute(name="u_job",153); font-weight:bold; background-color:inherit">protectedStringuJob;
- @XmlAttribute(name="u_mobile",153); font-weight:bold; background-color:inherit">protectedBigIntegeruMobile;
- @XmlAttribute(name="u_mood",153); font-weight:bold; background-color:inherit">protectedStringuMood;
- @XmlAttribute(name="u_name",153); font-weight:bold; background-color:inherit">protectedStringuName;
- @XmlAttribute(name="u_state",153); font-weight:bold; background-color:inherit">protectedStringuState;
- *GetsthevalueoftheuAvatarproperty.
- *@return
- *possibleobjectis
- *{@linkString}
- */
- publicStringgetUAvatar(){
- returnuAvatar;
- *SetsthevalueoftheuAvatarproperty.
- *@paramvalue
- *allowedobjectis
- voidsetUAvatar(Stringvalue){
- this.uAvatar=value;
- *GetsthevalueoftheuEmailproperty.
- publicStringgetUEmail(){
- returnuEmail;
- *SetsthevalueoftheuEmailproperty.
- voidsetUEmail(Stringvalue){
- this.uEmail=value;
- *GetsthevalueoftheuHometownproperty.
- publicStringgetUHometown(){
- returnuHometown;
- *SetsthevalueoftheuHometownproperty.
- voidsetUHometown(Stringvalue){
- this.uHometown=value;
- *GetsthevalueoftheuIdproperty.
- *{@linkBigInteger}
- publicBigIntegergetUId(){
- returnuId;
- *SetsthevalueoftheuIdproperty.
- voidsetUId(BigIntegervalue){
- this.uId=value;
- *GetsthevalueoftheuJobproperty.
- publicStringgetUJob(){
- returnuJob;
- *SetsthevalueoftheuJobproperty.
- voidsetUJob(Stringvalue){
- this.uJob=value;
- *GetsthevalueoftheuMobileproperty.
- publicBigIntegergetUMobile(){
- returnuMobile;
- *SetsthevalueoftheuMobileproperty.
- voidsetUMobile(BigIntegervalue){
- this.uMobile=value;
- *GetsthevalueoftheuMoodproperty.
- publicStringgetUMood(){
- returnuMood;
- *SetsthevalueoftheuMoodproperty.
- voidsetUMood(Stringvalue){
- this.uMood=value;
- *GetsthevalueoftheuNameproperty.
- publicStringgetUName(){
- returnuName;
- *SetsthevalueoftheuNameproperty.
- voidsetUName(Stringvalue){
- this.uName=value;
- *GetsthevalueoftheuStateproperty.
- publicStringgetUState(){
- returnuState;
- *SetsthevalueoftheuStateproperty.
- voidsetUState(Stringvalue){
- this.uState=value;
- }
另外也可以利用eclipse的jaxb项目自己的菜单功能也可以根据 java class自动生成xsd文件,如下图
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|