1.定義basexml,放置response屬性:java
使用註解app
@XmlAttribute
package com.zlg.test.xml; import javax.xml.bind.annotation.XmlAttribute; public class BaseXml { public static final String ERROR = "1"; public static final String SUCCESS = "0"; private String msgId; private String msg; private String sign; @XmlAttribute public String getMsgId() { return this.msgId; } public void setMsgId(String msgId) { this.msgId = msgId; } @XmlAttribute public String getMsg() { return this.msg; } public void setMsg(String msg) { this.msg = msg; } @XmlAttribute public String getSign() { return this.sign; } public void setSign(String sign) { this.sign = sign; } }
2.定義responsexml工具
使用註解,並命別名Response,以此咱們的類能夠根據業務須要命名,對外提供統一的出口Response測試
@XmlRootElement(name = "Response")
package com.zlg.test.xml; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "Response") public class ResponseXml extends BaseXml { private static final long serialVersionUID = 5594125247507960652L; private String code; private String description; private OrderResponseXml orderResponse; public String getCode() { return this.code; } public void setCode(String code) { this.code = code; } public String getDescription() { return this.description; } public void setDescription(String description) { this.description = description; } public OrderResponseXml getOrderResponse() { return this.orderResponse; } public void setOrderResponse(OrderResponseXml orderResponse) { this.orderResponse = orderResponse; } }
3.其餘層級xml定義ui
OrderResponseXmlthis
package com.zlg.test.xml; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "orderResponse") public class OrderResponseXml { private OrderXml order; public OrderXml getOrder() { return this.order; } public void setOrder(OrderXml order) { this.order = order; } }
OrderXmlspa
對於list元素須要添加以下註解,避免生成和讀取時缺失list中節點內容code
@XmlElementWrapper(name="ticketOrders") @XmlElement(name="ticketOrder")
package com.zlg.test.xml; import java.util.List; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "order") public class OrderXml { private static final long serialVersionUID = 7931221549662303872L; private String orderId; private String partnerId; private List<TicketOrderXml> ticketOrders; @XmlElement public String getOrderId() { return orderId; } public void setOrderId(String orderId) { this.orderId = orderId; } public String getPartnerId() { return partnerId; } public void setPartnerId(String partnerId) { this.partnerId = partnerId; } public List<TicketOrderXml> getTicketOrders() { return ticketOrders; } @XmlElementWrapper(name="ticketOrders") @XmlElement(name="ticketOrder") public void setTicketOrders(List<TicketOrderXml> ticketOrders) { this.ticketOrders = ticketOrders; } }
TicketOrderXmlxml
package com.zlg.test.xml; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "ticketOrder") public class TicketOrderXml { private String totalPrice; private String goodsName; public String getTotalPrice() { return totalPrice; } public void setTotalPrice(String totalPrice) { this.totalPrice = totalPrice; } public String getGoodsName() { return goodsName; } public void setGoodsName(String goodsName) { this.goodsName = goodsName; } }
備註:全部類,均須要使用註解@XmlRootElement(name = "ticketOrder") 能夠命別名blog
4.轉化工具類
package com.zlg.test.xml; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.io.StringWriter; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; /** * bean<->xml互轉工具類 * @author zlg * @date 2017年6月20日 * modify history */ public class XmlHelper { public static <T extends BaseXml> T toObj(Class<T> clazz, String xml) { try { JAXBContext context = JAXBContext .newInstance(new Class[] { clazz }); InputStream buf = new ByteArrayInputStream(xml.getBytes("UTF-8")); return (T) context.createUnmarshaller().unmarshal(buf); } catch (Exception e) { e.printStackTrace(); } return null; } public static <T extends BaseXml> String toXml(T obj) { try { StringWriter write = new StringWriter(); JAXBContext context = JAXBContext.newInstance(new Class[] { obj .getClass() }); context.createMarshaller().marshal(obj, write); return write.getBuffer().toString(); } catch (JAXBException e) { e.printStackTrace(); } return ""; } }
5.測試類
package com.zlg.test.xml; /** * 測試 * @author zlg * @date 2017年6月20日 * modify history */ public class XmlTest { public static void main(String[] args) { StringBuilder sb = new StringBuilder(); sb.append("<Response sign='1234567'>"); sb.append("<code>1000</code>"); sb.append("<description>測試</description>"); sb.append("<orderResponse>"); sb.append("<order>"); sb.append("<orderId>330182198804273139</orderId>"); sb.append("<partnerId>12345</partnerId>"); sb.append("<ticketOrders>"); for (int i = 0; i <= 1; i++) { sb.append("<ticketOrder>"); sb.append("<totalPrice>100</totalPrice>"); sb.append("<goodsName>測試票</goodsName>"); sb.append("</ticketOrder>"); } sb.append("</ticketOrders>"); sb.append("</order>"); sb.append("</orderResponse>"); sb.append("</Response>"); System.out.println("原始字符串:" + sb.toString()); ResponseXml respone = (ResponseXml) XmlHelper.toObj(ResponseXml.class, sb.toString()); System.out.println("獲取節點值:" + respone.getOrderResponse().getOrder().getOrderId()); System.out.println("獲取屬性:" + respone.getSign()); System.out.println("轉換字符串:" + XmlHelper.toXml(respone)); } }