通过JAXB,将javaBean转xml
最终效果:
代码内容:
1、请求的总体结构model(RequestModel)
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.XmlType;
@XmlRootElement(name = "Request")
@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlType(name = "", propOrder = { "head", "body" })
public class RequestModel<T extends Body<?>>{
protected Head head;
protected T body;
public T getBody() {
return body;
}
public void setBody(T body) {
this.body = body;
}
@XmlElement(name = "Head", required = true)
public Head getHead() {
return head;
}
public void setHead(Head value) {
this.head = value;
}
}
2、内容model(Body)
import javax.xml.bind.annotation.XmlMixed;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
@XmlRootElement
@XmlAccessorType(value = XmlAccessType.PROPERTY)
public class Body<T> {
private T body;
private String head;
public String getHead() {
return head;
}
public void setHead(String head) {
this.head = head;
}
@XmlMixed
public T getBody() {
return body;
}
public void setBody(T body) {
this.body = body;
}
}
3、xml中头结构Head
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
@XmlAccessorType(value = XmlAccessType.PROPERTY)
public class Head {
public String summary;
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
}
4、简单的测试bean(User、Customer)
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.XmlRootElement;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class User {
private String name;
private Integer age;
@XmlElementWrapper(name="customerList")
@XmlElement(name="customer")
private List<Customer> customer;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public List<Customer> getCustomer() {
return customer;
}
public void setCustomer(List<Customer> customer) {
this.customer = customer;
}
}
public class Customer {
private String commodity;
public String getCommodity() {
return commodity;
}
public void setCommodity(String commodity) {
this.commodity = commodity;
}
}
5、解析工具类(XmlUtil)
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
public class XmlUtil {
public static String toXmlString(Object objBean) throws Exception{
Body<Object> com = new Body<Object>();
com.setBody(objBean);
com.setHead("test");
RequestModel<Body<?>> re = new RequestModel<Body<?>>();
re.setBody(com);
Head h = new Head();
h.setSummary("test");
re.setHead(h);
JAXBContext context = JAXBContext.newInstance(RequestModel.class,Head.class,objBean.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
StringWriter writer = new StringWriter();
marshaller.marshal(re, writer);
return writer.toString();
}
}
6、测试main
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
//创建user对象与customer对象并赋值
User user = new User();
Customer customer1 = new Customer();
Customer customer2 = new Customer();
customer1.setCommodity("商品1");
customer2.setCommodity("商品2");
List<Customer> list = new ArrayList<Customer>();
list.add(customer1);
list.add(customer2);
user.setName("beyondLi");
user.setAge(23);
user.setCustomer(list);
try {
System.out.println(XmlUtil.toXmlString(user));;
} catch (Exception e) {
e.printStackTrace();
}
}
}