一、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接口的开发工作量。
三、jaxb代码举例
第一步:需要引入
javax.xml.bind.jar
第二步:编写java bean;
[java] view plain copy
- package
- import
- import
- import
- @XmlRootElement
- public class
- String name;
- int
- int
- public
- return
- }
- @XmlElement
- public void
- this.name = name;
- }
- public int
- return
- }
- @XmlElement
- public void setAge(int
- this.age = age;
- }
- public int
- return
- }
- @XmlAttribute
- public void setId(int
- this.id = id;
- }
- }
第三步:main方法把java bean转化为xml字符串
[java] view plain copy
- package
- import
- import
- import
- import
- public class
- public static void
- new
- 100);
- "mkyong");
- 29);
- try
- new File("C:\\file.xml");
- class);
- Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
- // output pretty printed
- true);
- jaxbMarshaller.marshal(customer, file);
- jaxbMarshaller.marshal(customer, System.out);
- catch
- e.printStackTrace();
- }
- }
- }
下面是输出:
[html] view plain copy
- <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
- <customer id="100">
- <age>29</age>
- <name>mkyong</name>
- </customer>
四、jaxb开发常用
jdk提供了xjc工具可以使xsd自动生成相应的java bean,这大大提高了开发的效率。同时,我们也可以使用
trang.jar把xml轻松转化为xsd。下面是使用的举例。
第一步:把数据库表映射为xml
[html] view plain copy
- <?xml version="1.0" encoding="UTF-8"?>
- <User u_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文件。在命令行执行:
[html] view plain copy
- java -jar D:\lib\trang.jar user.xml user.xsd
下面,是生成的User.java。
[java] view plain copy
- //
- // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6
- // See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
- // Any modifications to this file will be lost upon recompilation of the source schema.
- // Generated on: 2011.11.13 at 01:26:07 ���� CST
- //
- package
- import
- import
- import
- import
- import
- import
- import
- import
- import
- /**
- * <p>Java class for anonymous complex type.
- *
- * <p>The following schema fragment specifies the expected content contained within this class.
- *
- * <pre>
- * <complexType>
- * <complexContent>
- * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
- * <attribute name="u_avatar" use="required" type="{http://www.w3.org/2001/XMLSchema}NCName" />
- * <attribute name="u_email" use="required" type="{http://www.w3.org/2001/XMLSchema}anySimpleType" />
- * <attribute name="u_hometown" use="required" type="{http://www.w3.org/2001/XMLSchema}NCName" />
- * <attribute name="u_id" use="required" type="{http://www.w3.org/2001/XMLSchema}integer" />
- * <attribute name="u_job" use="required" type="{http://www.w3.org/2001/XMLSchema}NCName" />
- * <attribute name="u_mobile" use="required" type="{http://www.w3.org/2001/XMLSchema}integer" />
- * <attribute name="u_mood" use="required" type="{http://www.w3.org/2001/XMLSchema}NCName" />
- * <attribute name="u_name" use="required" type="{http://www.w3.org/2001/XMLSchema}NCName" />
- * <attribute name="u_state" use="required" type="{http://www.w3.org/2001/XMLSchema}NCName" />
- * </restriction>
- * </complexContent>
- * </complexType>
- * </pre>
- *
- *
- */
- @XmlAccessorType(XmlAccessType.FIELD)
- @XmlType(name = "")
- @XmlRootElement(name = "User")
- public class
- @XmlAttribute(name = "u_avatar", required = true)
- @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
- @XmlSchemaType(name = "NCName")
- protected
- @XmlAttribute(name = "u_email", required = true)
- @XmlSchemaType(name = "anySimpleType")
- protected
- @XmlAttribute(name = "u_hometown", required = true)
- @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
- @XmlSchemaType(name = "NCName")
- protected
- @XmlAttribute(name = "u_id", required = true)
- protected
- @XmlAttribute(name = "u_job", required = true)
- @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
- @XmlSchemaType(name = "NCName")
- protected
- @XmlAttribute(name = "u_mobile", required = true)
- protected
- @XmlAttribute(name = "u_mood", required = true)
- @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
- @XmlSchemaType(name = "NCName")
- protected
- @XmlAttribute(name = "u_name", required = true)
- @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
- @XmlSchemaType(name = "NCName")
- protected
- @XmlAttribute(name = "u_state", required = true)
- @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
- @XmlSchemaType(name = "NCName")
- protected
- /**
- * Gets the value of the uAvatar property.
- *
- * @return
- * possible object is
- * {@link String }
- *
- */
- public
- return
- }
- /**
- * Sets the value of the uAvatar property.
- *
- * @param value
- * allowed object is
- * {@link String }
- *
- */
- public void
- this.uAvatar = value;
- }
- /**
- * Gets the value of the uEmail property.
- *
- * @return
- * possible object is
- * {@link String }
- *
- */
- public
- return
- }
- /**
- * Sets the value of the uEmail property.
- *
- * @param value
- * allowed object is
- * {@link String }
- *
- */
- public void
- this.uEmail = value;
- }
- /**
- * Gets the value of the uHometown property.
- *
- * @return
- * possible object is
- * {@link String }
- *
- */
- public
- return
- }
- /**
- * Sets the value of the uHometown property.
- *
- * @param value
- * allowed object is
- * {@link String }
- *
- */
- public void
- this.uHometown = value;
- }
- /**
- * Gets the value of the uId property.
- *
- * @return
- * possible object is
- * {@link BigInteger }
- *
- */
- public
- return
- }
- /**
- * Sets the value of the uId property.
- *
- * @param value
- * allowed object is
- * {@link BigInteger }
- *
- */
- public void
- this.uId = value;
- }
- /**
- * Gets the value of the uJob property.
- *
- * @return
- * possible object is
- * {@link String }
- *
- */
- public
- return
- }
- /**
- * Sets the value of the uJob property.
- *
- * @param value
- * allowed object is
- * {@link String }
- *
- */
- public void
- this.uJob = value;
- }
- /**
- * Gets the value of the uMobile property.
- *
- * @return
- * possible object is
- * {@link BigInteger }
- *
- */
- public
- return
- }
- /**
- * Sets the value of the uMobile property.
- *
- * @param value
- * allowed object is
- * {@link BigInteger }
- *
- */
- public void
- this.uMobile = value;
- }
- /**
- * Gets the value of the uMood property.
- *
- * @return
- * possible object is
- * {@link String }
- *
- */
- public
- return
- }
- /**
- * Sets the value of the uMood property.
- *
- * @param value
- * allowed object is
- * {@link String }
- *
- */
- public void
- this.uMood = value;
- }
- /**
- * Gets the value of the uName property.
- *
- * @return
- * possible object is
- * {@link String }
- *
- */
- public
- return
- }
- /**
- * Sets the value of the uName property.
- *
- * @param value
- * allowed object is
- * {@link String }
- *
- */
- public void
- this.uName = value;
- }
- /**
- * Gets the value of the uState property.
- *
- * @return
- * possible object is
- * {@link String }
- *
- */
- public
- return
- }
- /**
- * Sets the value of the uState property.
- *
- * @param value
- * allowed object is
- * {@link String }
- *
- */
- public void
- this.uState = value;
- }
- }