jaxb xml生成与解析
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数据和处理函数。 常用属性: @XmlElement : XML 子节点 @XmlRootElement:XML 根目录节点 @XmlAttribute : XML 节点属性值 @XmlAccessorType :JAXB指定java类中的那些属性允许被访问生成xml或注入,其属性可填 @XmlAccessorOrder : @XmlTransient :不将java中的字段作为xml来解析或生成 @XmlJavaTypeAdapter:用于一些复杂的类,比如timestamp就要用自己写的adapter来转化 示例: 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,若不指定的话会产生如下异常: 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得,不过下次再写吧,如果你觉得楼主还有什么没写到位得,请在评论区写下你的其他方法,让你我他更多得人来学习 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |