本文主要是介绍XStream解析和读取xml报文,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
XStream读取和解析xml文件很简单,把xml结点定义好对象,最小的结点不用定义对象。不过这里要注意的是,当一个结点下有同样结点的多个时要把这个结点定义成List对象。如:
我的报文:
<request>
<order>
<supplier>100000000000001</supplier>
<buyer>上海航空1</buyer>
<orderId>O20120416095100</orderId>
<tickets>
<ticket>
<ticNo>E20120416095100</ticNo>
<tradeType>APPLY</tradeType>
<orderDate>2012-04-16</orderDate>
<departDate>dasdddddd</departDate>
<passenger>黄磊</passenger>
<fyNo>CA1520</fyNo>
<route>上海虹桥-北京</route>
<pnr>PNR_001</pnr>
<icsPnr>icsPnr</icsPnr>
<amt>128000</amt>
<memo>备注</memo>
<ext1>扩展字段1</ext1>
<ext2>扩展字段2</ext2>
</ticket>
<ticket>
<ticNo>2E20120416095100</ticNo>
<tradeType>APPLY</tradeType>
<orderDate>2012-04-16</orderDate>
<departDate>2012-04-16</departDate>
<passenger>黄磊</passenger>
<fyNo>CA1520</fyNo>
<route>上海虹桥-北京</route>
<pnr>PNR_001</pnr>
<icsPnr>icsPnr</icsPnr>
<amt>128000</amt>
<memo>备注</memo>
<ext1>扩展字段1</ext1>
<ext2>扩展字段2</ext2>
</ticket>
</tickets>
</order>
<topic>
<version>1.0</version>
<charset>utf-8</charset>
</topic>
<sign>
<signType>1</signType>
<signContent>sing</signContent>
</sign>
</request>
request将被定义成一个对象,旗下有三个对象order,topic,sign。order下面有对象也有最小借点,这个对象是List对象Ticket。这里要注意的是,定义的时候是 private List<Ticket> 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<Ticket> 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<Ticket> getTickets() {
return tickets;
}
public void setTickets(List<Ticket> 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<Ticket> 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<Ticket> ticketsList = new ArrayList<Ticket>();
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报文的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!