XStream解析和读取xml报文

XStream读取和解析xml文件很简单,把xml结点定义好对象,最小的结点不用定义对象。不过这里要注意的是,当一个结点下有同样结点的多个时要把这个结点定义成List对象。如:

我的报文:


 
    100000000000001
    上海航空1
    O20120416095100
   
     
        E20120416095100
        APPLY
        2012-04-16
        dasdddddd
        黄磊
        CA1520
        上海虹桥-北京
        PNR_001
        icsPnr
        128000
        备注
        扩展字段1
        扩展字段2
     

     
        2E20120416095100
        APPLY
        2012-04-16
        2012-04-16
        黄磊
        CA1520
        上海虹桥-北京
        PNR_001
        icsPnr
        128000
        备注
        扩展字段1
        扩展字段2
     

   

 

 
    1.0
    utf-8
 

 
    1
    sing
 

request将被定义成一个对象,旗下有三个对象order,topic,sign。order下面有对象也有最小借点,这个对象是List对象Ticket。这里要注意的是,定义的时候是 private List tickets;

tickets下有List对象Ticket多个,

 

Requset:

public class Request {
 private Order order;
 private Topic topic;
 private Sign sign;
 public Order getOrder() {
  return order;
 }
 public void setOrder(Order order) {
  this.order = order;
 }
 public Topic getTopic() {
  return topic;
 }
 public void setTopic(Topic topic) {
  this.topic = topic;
 }
 public Sign getSign() {
  return sign;
 }
 public void setSign(Sign sign) {
  this.sign = sign;
 }
 
}

 

Order:

public class Order {
 private String supplier;
 private String buyer;
 private String orderId;
 private List tickets;
 public String getSupplier() {
  return supplier;
 }
 public void setSupplier(String supplier) {
  this.supplier = supplier;
 }
 public String getBuyer() {
  return buyer;
 }
 public void setBuyer(String buyer) {
  this.buyer = buyer;
 }
 public String getOrderId() {
  return orderId;
 }
 public void setOrderId(String orderId) {
  this.orderId = orderId;
 }
 public List getTickets() {
  return tickets;
 }
 public void setTickets(List tickets) {
  this.tickets = tickets;
 }
 
}

Topic:

public class Topic {
 private String version;
 
 private String charset;

 public String getVersion() {
  return version;
 }

 public void setVersion(String version) {
  this.version = version;
 }

 public String getCharset() {
  return charset;
 }

 public void setCharset(String charset) {
  this.charset = charset;
 }
 
 
}

 

 

Sign:

public class Sign {
 private String signType;
 private String signContent;
 public String getSignType() {
  return signType;
 }
 public void setSignType(String signType) {
  this.signType = signType;
 }
 public String getSignContent() {
  return signContent;
 }
 public void setSignContent(String signContent) {
  this.signContent = signContent;
 }
 
 
}

 

Ticket:

public class Ticket {
 private String ticNo;
 private String tradeType;
 private String orderDate;
 private String departDate;
 private String passenger;
 private String fyNo;
 private String route;
 private String pnr;
 private String icsPnr;
 private Long amt;
 private String memo;
 private String ext1;
 private String ext2;
 public String getTicNo() {
  return ticNo;
 }
 public void setTicNo(String ticNo) {
  this.ticNo = ticNo;
 }
 public String getTradeType() {
  return tradeType;
 }
 public void setTradeType(String tradeType) {
  this.tradeType = tradeType;
 }
 public String getOrderDate() {
  return orderDate;
 }
 public void setOrderDate(String orderDate) {
  this.orderDate = orderDate;
 }
 public String getDepartDate() {
  return departDate;
 }
 public void setDepartDate(String departDate) {
  this.departDate = departDate;
 }
 public String getPassenger() {
  return passenger;
 }
 public void setPassenger(String passenger) {
  this.passenger = passenger;
 }
 public String getFyNo() {
  return fyNo;
 }
 public void setFyNo(String fyNo) {
  this.fyNo = fyNo;
 }
 public String getRoute() {
  return route;
 }
 public void setRoute(String route) {
  this.route = route;
 }
 public String getPnr() {
  return pnr;
 }
 public void setPnr(String pnr) {
  this.pnr = pnr;
 }
 public String getIcsPnr() {
  return icsPnr;
 }
 public void setIcsPnr(String icsPnr) {
  this.icsPnr = icsPnr;
 }
 public Long getAmt() {
  return amt;
 }
 public void setAmt(Long amt) {
  this.amt = amt;
 }
 public String getMemo() {
  return memo;
 }
 public void setMemo(String memo) {
  this.memo = memo;
 }
 public String getExt1() {
  return ext1;
 }
 public void setExt1(String ext1) {
  this.ext1 = ext1;
 }
 public String getExt2() {
  return ext2;
 }
 public void setExt2(String ext2) {
  this.ext2 = ext2;
 }
 
 
}

 

 

这样就定义好了对象。

现在只要用XStream来读取了。

 

public class TestXStram {
 
 public static void main(String[] args) {
  TestXStram t = new TestXStram();
  t.ticketsToXml();
  t.readTicketsXml();
 }

 public void readTicketsXml(){
  XStream xstream = new XStream(new DomDriver ());
  xstream.alias("topic", Topic.class);
  xstream.alias("order", Order.class);
  xstream.alias("sign", Sign.class);
  xstream.alias("ticket", Ticket.class);
  xstream.alias("request", Request.class);
  
  try {
   File file = new File("C:\\xml\\testXML.xml");
   InputStreamReader o = new FileReader(file);
   Reader reader = new InputStreamReader(new FileInputStream(file));
   
   //三种方法都可
//   xstream.fromXML(new FileInputStream(file));
//   xstream.fromXML(reader);
   Request request =  (Request) xstream.fromXML(o);
   Request request1 = request;
   List tsl = request.getOrder().getTickets();
   Ticket ticket = tsl.get(0);

//修改票中的一个字段
   ticket.setDepartDate("dasdddddd");
   

//requset和request1  修改一致
   String xml =  xstream.toXML(request1);
   System.out.println(xml);
   
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
 }
 
 public void ticketsToXml(){
   
  XStream xstream = new XStream(new DomDriver ());
  xstream.alias("topic", Topic.class);
  xstream.alias("order", Order.class);
  xstream.alias("sign", Sign.class);
  xstream.alias("ticket", Ticket.class);
  xstream.alias("request", Request.class);
  
  Request request = new Request();
  Order order = new Order();
  order.setBuyer("buyer");
  order.setOrderId("orderId");
  order.setSupplier("supplier");
  List ticketsList = new ArrayList();
  Ticket ticket = new Ticket();
  ticket.setAmt(21388L);
  ticketsList.add(ticket);
  order.setTickets(ticketsList);
  
  Ticket ticket2 = new Ticket();
  ticket2.setAmt(21388L);
  ticketsList.add(ticket2);
  order.setTickets(ticketsList);
  request.setOrder(order);
  
  String xml =  xstream.toXML(request);
  System.out.println(xml);
   
   
   
  
 }
}

 

你可能感兴趣的:(XStream解析和读取xml报文)