注解解释:
a、@XmlRootElement(name="sync_req"),将Java类或枚举映射成XML元素根节点,是唯一一个必须注解,name属性指定根节点名称,不指定默认为类名的小写。
b、@XmlType(propOrder = {"header","body"}),将Java类或枚举类型映射到XML模式类型,常与@XmlRootElement、@XmlAccessorType共用,propOrder属性定义字段生成的XML节点顺序。
c、@XmlElement(name="hdr"),xml节点
d、@XmlAttribute,xml中某一节点的属性
1、封装的xml操作工具类
package 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;
public class XMLUnit {
/**
* object转化为xml字符串
* @param obj
* @return
*/
public static String convertToXml(Object obj) {
// 创建输出流
StringWriter sw = new StringWriter();
sw.write("\n");
try {
// 利用jdk中自带的转换类实现
JAXBContext context = JAXBContext.newInstance(obj.getClass());
Marshaller marshaller = context.createMarshaller();
// 格式化xml输出的格式
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_ENCODING, "GB2312");
//去掉生成xml的默认报文头
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
// 将对象转换成输出流形式的xml
marshaller.marshal(obj, sw);
} catch (JAXBException e) {
e.printStackTrace();
}
return sw.toString();
}
/**
* 将xml转换为对象
* @param clazz
* @param xmlStr
* @return
*/
public static Object convertXmlStrToObject(Class clazz, String xmlStr) {
Object xmlObject = null;
try {
JAXBContext context = JAXBContext.newInstance(clazz);
// 进行将Xml转成对象的核心接口
Unmarshaller unmarshaller = context.createUnmarshaller();
StringReader sr = new StringReader(xmlStr);
xmlObject = unmarshaller.unmarshal(sr);
} catch (JAXBException e) {
e.printStackTrace();
}
return xmlObject;
}
}
2、实体类
package request;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlRootElement(name="sync_req")
@XmlType(propOrder = {"header","body"})
public class RequestClass {
/*
private String ver="1.0.0";
private Header header;
private Body body;
public RequestClass(){
super();
}
@XmlAttribute
public String getVer() {
return ver;
}
public void setVer(String ver) {
this.ver = ver;
}
@XmlElement(name="hdr")
public Header getHeader() {
return header;
}
public void setHeader(Header header) {
this.header = header;
}
@XmlElement
public Body getBody() {
return body;
}
public void setBody(Body body) {
this.body = body;
}
}
package request;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlRootElement(name="hdr")
@XmlType(propOrder = {"id","pwd"})
public class Header {
/*
private String id;
private String pwd;
public Header() {
super();
}
@XmlElement
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@XmlElement
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
package request;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="request")
public class Request {
private Msisdn msisdn;
@XmlElement
public Msisdn getMsisdn() {
return msisdn;
}
public void setMsisdn(Msisdn msisdn) {
this.msisdn = msisdn;
}
}
package request;
import java.util.List;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlRootElement(name="msisdn")
public class Msisdn {
/*
private String seq_id;
private String action;
private String business_id;
private String serviceid;
private List
@XmlAttribute
public String getSeq_id() {
return seq_id;
}
public void setSeq_id(String seq_id) {
this.seq_id = seq_id;
}
@XmlAttribute
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
@XmlAttribute
public String getBusiness_id() {
return business_id;
}
public void setBusiness_id(String business_id) {
this.business_id = business_id;
}
@XmlAttribute
public String getServiceid() {
return serviceid;
}
public void setServiceid(String serviceid) {
this.serviceid = serviceid;
}
@XmlElement(name="property")
public List
return propertys;
}
public void setPropertys(List
this.propertys = propertys;
}
}
3、测试类
package xml;
import java.util.ArrayList;
import java.util.List;
import request.Body;
import request.Header;
import request.Msisdn;
import request.Property;
import request.Request;
import request.RequestClass;
import response.ResponseClass;
public class XmlToClassTest {
public static void main(String[] args) throws Exception {
convertToXml();
}
public static void convertToXml(){
RequestClass requestClass = new RequestClass();
Header header = new Header();
header.setId("10001");
header.setPwd("password");
requestClass.setHeader(header);
Body body = new Body();
Request request = new Request();
Msisdn msisdn = new Msisdn();
msisdn.setSeq_id("10001");
msisdn.setAction("A");
msisdn.setBusiness_id("8900000002");
msisdn.setServiceid("cldw");
List
Property property1 = new Property();
property1.setKey("msisdn");
property1.setValue("13945678932");
list.add(property1);
Property property2 = new Property();
property2.setKey("ue_type");
property2.setValue("1");
list.add(property2);
Property property3 = new Property();
property3.setKey("status");
property3.setValue("A");
list.add(property3);
Property property4 = new Property();
property4.setKey("reserve_1");
property4.setValue("13945670005");
list.add(property4);
Property property5 = new Property();
property5.setKey("reserve_2");
property5.setValue("13945670006");
list.add(property5);
Property property6 = new Property();
property6.setKey("locationTime");
property6.setValue("3");
list.add(property6);
Property property7 = new Property();
property7.setKey("ue_id");
property7.setValue("1394567824234932");
list.add(property7);
Property property8 = new Property();
property8.setKey("ue_model");
property8.setValue("1394asdf");
list.add(property8);
Property property9 = new Property();
property9.setKey("auto_num");
property9.setValue("辽A12345");
list.add(property9);
msisdn.setPropertys(list);
request.setMsisdn(msisdn);
body.setRequest(request);
requestClass.setBody(body);
String response = XMLUnit.convertToXml(requestClass);
System.out.println("xml请求数据:");
System.out.println(response);
Object obj = XMLUnit.convertXmlStrToObject(RequestClass.class, response);
System.out.println(obj);
}
}