JAXB能够使用Jackson对JAXB注解的支持实现(jackson-module-jaxb-annotations),既方便生成XML,也方便生成JSON,这样一来可以更好的标志可以转换为JSON对象的JAVA类。JAXB允许JAVA人员将JAVA类映射为XML表示方式,常用的注解包括:@XmlRootElement,@XmlElement等等。 JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术。该过程中,JAXB也提供了将XML实例文档反向生成Java对象树的方法,并能将Java对象树的内容重新写到XML实例文档。从另一方面来讲,JAXB提供了快速而简便的方法将XML模式绑定到Java表示,从而使得Java开发者在Java应用程序中能方便地结合XML数据和处理函数。
常用属性:
@XmlType :注解用于标注类或枚举类型,用它标注的类在映射后的 schema 中中会以一个 XML 复杂数据类型的形式出现。我们可以通过 @XmlType 注解的 name 属性来定制映射的 XML 数据类型的名称,用 propOrder 属性来定制映射后的复杂数据类型的内容顺序等
@XmlElement : XML 子节点
@XmlRootElement:XML 根目录节点
@XmlAttribute : XML 节点属性值
@XmlAccessorType :JAXB指定java类中的那些属性允许被访问生成xml或注入,其属性可填
*XmlAccessType.PROPERTY:java对象中所有通过getter/setter方式访问的成员变量;
XmlAccessType.PUBLIC_MEMBER:java对象中所有的public访问权限的成员变量和通过getter/setter方式访问的成员变量;
XmlAccessType.NONE:java对象的所有属性都不映射为xml的元素。
XmlAccessType.FIELD:java对象中的所有成员变量;*
@XmlAccessorOrder :
使用@XmlAccessorOrder 包括XmlAccessorOrder.UNDEFINED XmlAccessorOrder.ALPHABETICAL两个值,默认为UNDEFINED(无序),XmlAccessorOrder.ALPHABETICAL是指按属性的字母顺序排序。
当@XmlAccessorOrder定义在一个package之上时候(Package annotations must be in file package-info.Java),此包下的所有类都遵守@XmlAccessorOrder定义的规则;当它定义在类之上时,只有该类的内容遵守规则。
就近原则:如果出现在package之上使用@XmlAccessorOrder并且在该包下的某个类又使用了@XmlAccessorOrder,那在该类上定义的@XmlAccessorOrder规则才起作用,如在package上声明@XmlAccessorOrder(XmlAccessOrder.ALPHABETICAL),而在某类上声明为UNDEFINED,则该类遵守的规则为UNDEFINED。
使用@XmlType.propOrder:
propOrder可以定义class中字段出现在xml中的顺序,class中所有public的字段必须列的propOrder的参数列表中,如果不想将某public字段列在列表中,可在字段上使用@XmlTransient or @XmlAttribute注解标识。
默认的内容顺序为@XmlType.propOrder is {} or {“”}(not active 可认为无序?),在这种情况下,@XmlAccessorOrder 注解具有优先权。当@XmlAccessorOrder 与@XmlType.propOrder同时出现时,propOrder总是具有优先权(最终起作用)。
@XmlTransient :不将java中的字段作为xml来解析或生成
@XmlJavaTypeAdapter:用于一些复杂的类,比如timestamp就要用自己写的adapter来转化
@Temporal(TemporalType.XXXX) –>JPA中的时间处理注解,非JAXB
@XmlElementWrapper :用来对xml做进一步包装
示例:
@XmlRootElement:
package com.cmh.beans;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Customer {
private int id;
private String name;
private String pwd;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
工具类:
/** * JavaBean转换成xml * @param obj * @param encoding * @return */
public static String convertToXml(Object obj,String encoding) {
String result = null;
try {
JAXBContext context = JAXBContext.newInstance(obj.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
marshaller.setProperty(Marshaller.JAXB_ENCODING,encoding);
StringWriter writer = new StringWriter();
marshaller.marshal(obj,writer);
result = writer.toString();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
@SuppressWarnings("unchecked")
public static <T> T converyToJavaBean(String xml,Class<T> c) {
T t = null;
try {
JAXBContext context = JAXBContext.newInstance(c);
Unmarshaller unmarshaller = context.createUnmarshaller();
t = (T) unmarshaller.unmarshal(new StringReader(xml));
} catch (Exception e) {
e.printStackTrace();
}
return t;
}
测试代码:
Customer custommer = new Customer();
custommer.setId(0);
System.out.println(XMLUtil.convertToXml(custommer,"utf-8"));
测试结果
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<customer>
<id>0</id>
</customer>
@XmlRootElement:
测试用例:
package com.cmh.beans;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
@XmlElement(name="customerid")
private int id;
private String name;
private String pwd;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
这里加@XmlAccessorType是为了让jaxb注解知道取那个值来生成xml,若不指定的话会产生如下异常:
com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
Class has two properties of the same name "id"
this problem is related to the following location:
at public int com.cmh.beans.Customer.getId()
at com.cmh.beans.Customer
this problem is related to the following location:
at private int com.cmh.beans.Customer.id
at com.cmh.beans.Customer
测试结果如下:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<customer>
<customerid>0</customerid>
</customer>
@XmlAttribute
测试用例:
package com.cmh.beans;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAnyAttribute;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
@XmlElement(name="customerid")
private int id;
@XmlAttribute
private String name;
@XmlAttribute
private String pwd;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
测试用例:
Customer custommer = new Customer();
custommer.setId(0);
custommer.setName("22");
custommer.setPwd("x");
System.out.println(XMLUtil.convertToXml(custommer,"utf-8"));
结果:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<customer pwd="x" name="22">
<customerid>0</customerid>
</customer>
如果我们的类里面包含了集合类会产生什么样的结果呢
测试用例:
package com.cmh.beans;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Person {
private int id;
private String address;
private String email;
private int age;
@XmlElement(name="custommer")
private List<Customer> custommer;
public List<Customer> getCustommer() {
return custommer;
}
public void setCustommer(List<Customer> custommer) {
this.custommer = custommer;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package com.cmh.beans;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAnyAttribute;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
@XmlElement(name="customerid")
private int id;
@XmlAttribute
private String name;
@XmlAttribute
private String pwd;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
测试代码:
Customer custommer = new Customer();
custommer.setId(0);
custommer.setName("22");
custommer.setPwd("x");
Person p = new Person();
List<Customer> custoomers = new ArrayList<Customer>();
custoomers.add(custommer);
p.setAddress("232");
p.setAge(22);
p.setEmail("2321");
p.setCustommer(custoomers);
System.out.println(XMLUtil.convertToXml(p,"utf-8"));
测试结果:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<person>
<id>0</id>
<address>232</address>
<email>2321</email>
<age>22</age>
<custommer pwd="x" name="22">
<customerid>0</customerid>
</custommer>
</person>
@XmlElements的使用,可以在list上进行赋值,记住该list不确定类型比如直接声明list而不是list,这样我们就可以为后面加进来的类型进行xml解析了,示例
package com.cmh.beans;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Person {
private int id;
private String address;
private String email;
private int age;
@XmlElements( value = {
@XmlElement(name="custommer",type=Customer.class),@XmlElement(name="user",type=User.class)
})
private List custommer;
public List getCustommer() {
return custommer;
}
public void setCustommer(List custommer) {
this.custommer = custommer;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package com.cmh.beans;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAnyAttribute;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
@XmlElement(name="customerid")
private int id;
@XmlAttribute
private String name;
@XmlAttribute
private String pwd;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
package com.cmh.beans;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
@XmlAccessorType(XmlAccessType.FIELD)
public class User {
@XmlElement(name="customerid")
private int id;
@XmlAttribute
private String name;
@XmlAttribute
private String pwd;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
测试代码:
Customer custommer = new Customer();
custommer.setId(0);
custommer.setName("22");
custommer.setPwd("x");
User user = new User();
user.setId(0);
user.setName("22");
user.setPwd("x");
Person p = new Person();
List custoomers = new ArrayList<Customer>();
custoomers.add(custommer);
custoomers.add(user);
p.setAddress("232");
p.setAge(22);
p.setEmail("2321");
p.setCustommer(custoomers);
System.out.println(XMLUtil.convertToXml(p,"utf-8"));
测试结果:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<person>
<id>0</id>
<address>232</address>
<email>2321</email>
<age>22</age>
<custommer pwd="x" name="22">
<customerid>0</customerid>
</custommer>
<user pwd="x" name="22">
<customerid>0</customerid>
</user>
</person>
@XmlElementWrapper(name=”wrap”):使用示例
package com.cmh.beans;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Person {
private int id;
private String address;
private String email;
private int age;
@XmlElementWrapper(name="wrap")
@XmlElements( value = {
@XmlElement(name="custommer",type=User.class)
})
private List custommer;
public List getCustommer() {
return custommer;
}
public void setCustommer(List custommer) {
this.custommer = custommer;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
测试结果:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<person>
<id>0</id>
<address>232</address>
<email>2321</email>
<age>22</age>
<wrap>
<custommer pwd="x" name="22">
<customerid>0</customerid>
</custommer>
<user pwd="x" name="22">
<customerid>0</customerid>
</user>
</wrap>
</person>
由于时间有点晚了,我得睡觉了,本来还想写以下xml to bean得,不过下次再写吧,如果你觉得楼主还有什么没写到位得,请在评论区写下你的其他方法,让你我他更多得人来学习