XStream解析和讀取xml報文

2021-06-05 20:27:41 字數 2792 閱讀 7027

xstream讀取和解析xml檔案很簡單,把xml結點定義好物件,最小的結點不用定義物件。不過這裡要注意的是,當乙個結點下有同樣結點的多個時要把這個結點定義成list物件。如:

我的報文:

100000000000001

上海航空1

o20120416095100

e20120416095100

2012-04-16

dasdddddd

黃磊ca1520

上海虹橋-北京

pnr_001

icspnr

128000

備註擴充套件欄位1

擴充套件欄位2

2e20120416095100

2012-04-16

2012-04-16

黃磊ca1520

上海虹橋-北京

pnr_001

icspnr

128000

備註擴充套件欄位1

擴充套件欄位2

1.0utf-8

1sing

request將被定義成乙個物件,旗下有三個物件order,topic,sign。order下面有物件也有最小借點,這個物件是list物件ticket。這裡要注意的是,定義的時候是 private listtickets;

tickets下有list物件ticket多個,

requset:

public class request

public void setorder(order order)

public topic gettopic()

public void settopic(topic topic)

public sign getsign()

public void setsign(sign sign)

}order:

public class order

public void setsupplier(string supplier)

public string getbuyer()

public void setbuyer(string buyer)

public string getorderid()

public void setorderid(string orderid)

public listgettickets()

public void settickets(listtickets)

}topic:

public class topic

public void setversion(string version)

public string getcharset()

public void setcharset(string charset)

}sign:

public class sign

public void setsigntype(string signtype)

public string getsigncontent()

public void setsigncontent(string signcontent)

}ticket:

public class ticket

public void setticno(string ticno)

public string gettradetype()

public void settradetype(string tradetype)

public string getorderdate()

public void setorderdate(string orderdate)

public string getdepartdate()

public void setdepartdate(string departdate)

public string getpassenger()

public void setpassenger(string passenger)

public string getfyno()

public void setfyno(string fyno)

public string getroute()

public void setroute(string route)

public string getpnr()

public void setpnr(string pnr)

public string geticspnr()

public void seticspnr(string icspnr)

public long getamt()

public void setamt(long amt)

public string getmemo()

public void setmemo(string memo)

public string getext1()

public void setext1(string ext1)

public string getext2()

public void setext2(string ext2)

}這樣就定義好了物件。

現在只要用xstream來讀取了。

public class testxstram

public void readticketsxml() catch (filenotfoundexception e)

}public void ticketstoxml()

}

XStream解析xml檔案

1.xstream的基本使用 package test public class myxstreamuse public class company public class person輸出結果 abc2012 2 3a23 b242.常用函式和相應註解 outstream.alias compa...

xStream解析xml檔案

以 為例 xml解析需要用到兩個註解,乙個屬性 類註解,乙個是集合註解 先看bean.class 寫xml和json的bean.class的需要特別注意的一點是 xml.class類裡的class類是靜態的,要不然會報錯 xstreamalias root public class bean 快 s...

5分鐘學會Xstream解析XML檔案

下面是乙個簡單例項,比如有個這樣的xml檔案 用xstream實現該檔案的解析很簡單,首先,編寫乙個userprofile類,如下 public class userprofile public list getusers public void adduser user user jack jac...