JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术。JAXB 已经是jdk的组成部分,不需要另外引入jar包
(1)@XmlRootElement(name = "response") 将Java类或枚举类型映射到XML元素。
(2) @XmlElement(name = "result") 将Java类的一个属性映射到与属性同名的一个XML元素。
(3) @XmlAttribute(name="code") 将Java类的一个属性映射到与属性同名的一个XML属性。
(4) @XmlValue(name="result") 将Java类的一个属性映射到XML元素之间的值上(例:
以一个比较复杂,并且可以涵盖大部分内容的例子(代码较多,可以直接拷贝到自己工程中使用):
工具类
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
/**
* jaxb工具类
*/
public class JaxbUtil {
// 多线程安全的Context.
private JAXBContext jaxbContext;
/**
* @param types
* 所有需要序列化的Root对象的类型.
*/
public JaxbUtil(Class>... types) {
try {
jaxbContext = JAXBContext.newInstance(types);
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
/**
* 对象转xml
*/
public String toXml(Object root, String encoding) {
try {
StringWriter writer = new StringWriter();
createMarshaller(encoding).marshal(root, writer);
return writer.toString();
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
/**
* xml转对象
*/
@SuppressWarnings("unchecked")
public T fromXml(String xml) {
try {
StringReader reader = new StringReader(xml);
return (T) createUnmarshaller().unmarshal(reader);
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
/**
* 创建Marshaller, 设定encoding(可为Null).
*/
public Marshaller createMarshaller(String encoding) {
try {
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
if (!encoding.equals("") && encoding!=null) {
marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
}
return marshaller;
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
/**
* 创建UnMarshaller.
*/
public Unmarshaller createUnmarshaller() {
try {
return jaxbContext.createUnmarshaller();
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
}
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;
/**
*
* 获取组织结构xml对象
*/
@XmlRootElement(name = "response")
public class GetOrgResponse
{
Result result;
Message message;
public Result getResult()
{
return result;
}
@XmlElement(name = "result")
public void setResult(Result result)
{
this.result = result;
}
public Message getMessage()
{
return message;
}
@XmlElement(name = "message")
public void setMessage(Message message)
{
this.message = message;
}
@XmlRootElement(name = "message")
public static class Message
{
Company company;
public Company getCompany()
{
return company;
}
@XmlElement(name = "company")
public void setCompany(Company company)
{
this.company = company;
}
}
@XmlRootElement(name = "result")
public static class Result
{
String code;
String result;
public String getCode()
{
return code;
}
@XmlAttribute
public void setCode(String code)
{
this.code = code;
}
public String getResult()
{
return result;
}
@XmlValue
public void setResult(String result)
{
this.result = result;
}
}
}
import java.util.List;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
* 企业信息xml对象
*/
@XmlRootElement(name = "company")
public class Company
{
String id;
String name;
String enter_id;
String ver;
String short_name;
String privacy;
List members;
List departments;
public String getId()
{
return id;
}
@XmlAttribute(name = "id")
public void setId(String id)
{
this.id = id;
}
public String getName()
{
return name;
}
@XmlAttribute(name = "name")
public void setName(String name)
{
this.name = name;
}
public String getEnter_id()
{
return enter_id;
}
@XmlAttribute(name = "enter_id")
public void setEnter_id(String enter_id)
{
this.enter_id = enter_id;
}
public String getVer()
{
return ver;
}
@XmlAttribute(name = "ver")
public void setVer(String ver)
{
this.ver = ver;
}
public String getShort_name()
{
return short_name;
}
@XmlAttribute(name = "short_name")
public void setShort_name(String short_name)
{
this.short_name = short_name;
}
public String getPrivacy()
{
return privacy;
}
@XmlAttribute(name = "privacy")
public void setPrivacy(String privacy)
{
this.privacy = privacy;
}
public List getMembers()
{
return members;
}
@XmlElement(name = "member")
public void setMembers(List members)
{
this.members = members;
}
public List getDepartments()
{
return departments;
}
@XmlElement(name = "dep")
public void setDepartments(List departments)
{
this.departments = departments;
}
}
import java.util.List;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
* 部门xml对象
*/
@XmlRootElement(name = "dep")
public class Department
{
String id;
String name;
String clan_order;
List members;
List departments;
public String getId()
{
return id;
}
@XmlAttribute(name = "id")
public void setId(String id)
{
this.id = id;
}
public String getName()
{
return name;
}
@XmlAttribute(name = "name")
public void setName(String name)
{
this.name = name;
}
public String getClan_order()
{
return clan_order;
}
@XmlAttribute(name = "clan_order")
public void setClan_order(String clan_order)
{
this.clan_order = clan_order;
}
public List getMembers()
{
return members;
}
@XmlElement(name = "member")
public void setMembers(List members)
{
this.members = members;
}
public List getDepartments()
{
return departments;
}
@XmlElement(name = "dep")
public void setDepartments(List departments)
{
this.departments = departments;
}
}
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
/**
* 组织结构中成员对象
*/
@XmlRootElement(name = "member")
public class Member
{
String name;
String nbr;
String sex;
String ext;
String sort_no;
String login_name;
String pic_url;
String headship;
public String getName()
{
return name;
}
@XmlAttribute(name = "name")
public void setName(String name)
{
this.name = name;
}
public String getNbr()
{
return nbr;
}
@XmlAttribute(name = "nbr")
public void setNbr(String nbr)
{
this.nbr = nbr;
}
public String getSex()
{
return sex;
}
@XmlAttribute(name = "sex")
public void setSex(String sex)
{
this.sex = sex;
}
public String getExt()
{
return ext;
}
@XmlAttribute(name = "ext")
public void setExt(String ext)
{
this.ext = ext;
}
public String getSort_no()
{
return sort_no;
}
@XmlAttribute(name = "sort_no")
public void setSort_no(String sort_no)
{
this.sort_no = sort_no;
}
public String getLogin_name()
{
return login_name;
}
@XmlAttribute(name = "login_name")
public void setLogin_name(String login_name)
{
this.login_name = login_name;
}
public String getPic_url()
{
return pic_url;
}
@XmlAttribute(name = "pic_url")
public void setPic_url(String pic_url)
{
this.pic_url = pic_url;
}
public String getHeadship()
{
return headship;
}
@XmlAttribute(name = "headship")
public void setHeadship(String headship)
{
this.headship = headship;
}
}
测试:
public class Test
{
public static void main(String[] args)
throws Exception
{
JaxbUtil resultBinder = new JaxbUtil(GetOrgResponse.class);
String xml =
""
+ "成功 "
+ " "
+ " "
+ " "
+ ""
+ " "
+ " "
+ " " + " ";
GetOrgResponse res = resultBinder.fromXml(xml);
System.out.println(res.getResult().getCode());
System.out.println(res.getResult().getResult());
System.out.println(res.getMessage().getCompany().getName());
System.out.println(res.getMessage().getCompany().getEnter_id());
System.out.println(res.getMessage().getCompany().getVer());
if (res.getMessage().getCompany().getMembers() != null)
{
for (Member member : res.getMessage().getCompany().getMembers())
{
System.out.println(member.getNbr() + "=" + member.getName() + "=" + member.getPic_url());
}
}
if (res.getMessage().getCompany().getDepartments() != null)
{
for (Department dep : res.getMessage().getCompany().getDepartments())
{
System.out.println(dep.getId() + "=" + dep.getName());
}
}
if (res.getMessage().getCompany().getMembers() != null)
{
for (Member member : res.getMessage().getCompany().getMembers())
{
System.out.println(member.getNbr() + "=" + member.getName() + "=" + member.getPic_url());
}
}
}
}
代码下载地址
(1)属性节点有下划线时必须这么写@XmlAttribute(name = "parentDepId")
如果只写@XmlAttribute将获取不到带下划线属性或者节点的值
(2)如果不想要某个属性或者节点,将此属性设为null即可
在使用jaxb从bean转换为xml过程中,默认会将bean中属性为null的属性过滤掉