java 中XML 的解析,转换

 

import java.io.StringReader;

import java.io.StringWriter;

import javax.xml.bind.JAXBContext;

import javax.xml.bind.Marshaller;

import javax.xml.bind.Unmarshaller;

 

public class XmlParser {

 

/***

* 把对象格式为XML字符串

* @param obj

* @param c

* @return

* @throws Exception

*/

public static <T> String format(T obj, Class<T> c) throws Exception {

JAXBContext jaxbContext = JAXBContext.newInstance(c);

Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

StringWriter out = new StringWriter();

jaxbMarshaller.marshal(obj, out);

return out.toString();

}

 

 

/****

* 把XML字符串解析成对象

* @return

* @throws Exception

*/

public static <T> T parse(String xml, Class<T> c) throws Exception {

JAXBContext jaxbContext = JAXBContext.newInstance(c);

Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

@SuppressWarnings("unchecked")

T result = (T) jaxbUnmarshaller.unmarshal(new StringReader(xml));

return result;

}

 

 

/*

 * 测试方法

 * /

  public static void main(String[] args) throws Exception{

System.out.println(format(Order.mock(), Order.class));

String xml = format(Order.mock(), Order.class);

System.out.println(parse(xml, Order.class));

}

 

}

----------------------------------------------------------------------------------------------------------------------

 

对象类Order

 

import javax.xml.bind.annotation.XmlElement;

import javax.xml.bind.annotation.XmlRootElement;

 

@XmlRootElement(name="Order")

public class Order implements Serializable{

/**

*/

private static final long serialVersionUID = 1L;

private String epcode;// 运单号,部分发货方式必填

private String userid;// 客户号

private String channel;// 发货方式

private String userOrderNumber;// 客户订单号

private String sendDate;// 发货日期

private String quantity;// 货品数量

private String packageNo;// 包裹号

private String insure;// 是否需要保险

private String memo;// 备注。会出现在拣货单上

private Goods goods;

 

@XmlElement(name = "Epcode")

public String getEpcode() {

return epcode;

}

public void setEpcode(String epcode) {

this.epcode = epcode;

}

@XmlElement(name = "Userid")

public String getUserid() {

return userid;

}

public void setUserid(String userid) {

this.userid = userid;

}

 

@XmlElement(name="Channel")

public String getChannel() {

return channel;

}

public void setChannel(String channel) {

this.channel = channel;

}

 

@XmlElement(name="OrderNumber")

public String getUserOrderNumber() {

return userOrderNumber;

}

public void setUserOrderNumber(String userOrderNumber) {

this.userOrderNumber = userOrderNumber;

}

 

@XmlElement(name="SendDate")

public String getSendDate() {

return sendDate;

}

public void setSendDate(String sendDate) {

this.sendDate = sendDate;

}

 

@XmlElement(name="Quantity")

public String getQuantity() {

return quantity;

}

public void setQuantity(String quantity) {

this.quantity = quantity;

}

 

@XmlElement(name="PackageNo")

public String getPackageNo() {

return packageNo;

}

public void setPackageNo(String packageNo) {

this.packageNo = packageNo;

}

 

@XmlElement(name="Insure")

public String getInsure() {

return insure;

}

public void setInsure(String insure) {

this.insure = insure;

}

 

@XmlElement(name="Memo")

public String getMemo() {

return memo;

}

public void setMemo(String memo) {

this.memo = memo;

}

 

@XmlElement(name="Goods")

public Goods getGoods() {

return goods;

}

public void setGoods(Goods goods) {

this.goods = goods;

}

 

 

 

@Override

public String toString() {

return "Order [epcode=" + epcode + ", userid=" + userid + ", channel="

+ channel + ", userOrderNumber=" + userOrderNumber

+ ", sendDate=" + sendDate + ", quantity=" + quantity

+ ", packageNo=" + packageNo + ", insure=" + insure + ", memo="

+ memo + ", goods=" + goods + "]";

}

 

/***

* 填充测试数据

* @return

*/

public static Order mock() {

Order order = new Order();

order.setChannel("amazion");

order.setEpcode("156889635520");

order.setUserid("1002");

order.setPackageNo("20160808");

 

Goods goods = new Goods();

goods.setSku("10008987666");

goods.setGoodsName("袜子");

goods.setWeight("12KG");

goods.setDeclaredCurrency("US");

order.setGoods(goods);

return order;

}

-----------------------------------------------------------------------------------------------------------------

对象类Goods

import javax.xml.bind.annotation.XmlElement;

import javax.xml.bind.annotation.XmlRootElement;

 

@XmlRootElement(name="Goods")

public class Goods {

 

private String sku;

private String goodsName;

private String weight;

private String declaredCurrency;

 

@XmlElement(name="Sku")

public String getSku() {

return sku;

}

public void setSku(String sku) {

this.sku = sku;

}

@XmlElement(name="GoodsName")

public String getGoodsName() {

return goodsName;

}

public void setGoodsName(String goodsName) {

this.goodsName = goodsName;

}

@XmlElement(name="Weight")

public String getWeight() {

return weight;

}

public void setWeight(String weight) {

this.weight = weight;

}

@XmlElement(name="DeclaredCurrency")

public String getDeclaredCurrency() {

return declaredCurrency;

}

public void setDeclaredCurrency(String declaredCurrency) {

this.declaredCurrency = declaredCurrency;

}

 

}

 

 

你可能感兴趣的:(java,xml)