XStream 用法詳解 java 類與 XML 互換
現在 WEB資料交換的時代,傳送XML目前是一個比較流行的方式,具有統一的規則約束,為實現後台介面提供了一個很方便的實現。
我編寫了一個 接收XML並轉換成所需要的Object類的 小例子,希望能夠對做互連網資料轉送、介面調用的朋友有所協助。
首先要匯入jar包xstream-1.4.3-sources.jar 和 xmlpull-1.1.3.1.jar 兩個包;
其次是預備一個 XML 案例
<config><span style="white-space:pre"></span><client type="8888" osversion="9999" version="123" oemtag="5555" area="areacode"/><span style="white-space:pre"></span><protocol>1.10</protocol><span style="white-space:pre"></span><sign value="asdfasdf"/><span style="white-space:pre"></span><vientiance version="version"/></config>
其次 就是 按照 XML節點的順序 從外 到內 編寫 java PO類,此執行個體的目的是將上面的XML轉換為 AllnewstateRQ 這個實體類,然後從裡面列印出測試資料。
下面依次是 config節點 對應的 AllnewstateRQ類 ; client 節點 對應的 Client 類;sign 節點對應的 Sign類;vientiance 節點 對應的 Vientiance類。
package com.wgq.test09_xml;import com.thoughtworks.xstream.annotations.XStreamAlias;@XStreamAlias("config")public class AllnewstateRQ {//當節點下有獨立屬性的時候,需要建立一個獨立的類用來儲存節點內的屬性private Client client = new Client();private Sign sign = new Sign();private Vientiance vientiance = new Vientiance();//當節點下沒有屬性,直接由StringValue的時候可直接建立String類型屬性private String protocol;public Client getClient() {return client;}public void setClient(Client client) {this.client = client;}public Sign getSign() {return sign;}public void setSign(Sign sign) {this.sign = sign;}public Vientiance getVientiance() {return vientiance;}public void setVientiance(Vientiance vientiance) {this.vientiance = vientiance;}public String getProtocol() {return protocol;}public void setProtocol(String protocol) {this.protocol = protocol;}}
package com.wgq.test09_xml;import com.thoughtworks.xstream.annotations.XStreamAlias;import com.thoughtworks.xstream.annotations.XStreamAsAttribute;@XStreamAlias("client")public class Client {@XStreamAsAttribute//對屬性值進行註解private String type;//此時類中的屬性名稱要和xml內的屬性名稱相對應@XStreamAsAttributeprivate String osversion;@XStreamAsAttributeprivate String version;@XStreamAsAttributeprivate String oemtag;@XStreamAsAttributeprivate String area;public String getType() {return type;}public void setType(String type) {this.type = type;}public String getOsversion() {return osversion;}public void setOsversion(String osversion) {this.osversion = osversion;}public String getVersion() {return version;}public void setVersion(String version) {this.version = version;}public String getOemtag() {return oemtag;}public void setOemtag(String oemtag) {this.oemtag = oemtag;}public String getArea() {return area;}public void setArea(String area) {this.area = area;}}
package com.wgq.test09_xml;import com.thoughtworks.xstream.annotations.XStreamAlias;import com.thoughtworks.xstream.annotations.XStreamAsAttribute;@XStreamAlias("sign") //此處要對應節點的名稱public class Sign {@XStreamAsAttributeprivate String value;//此處對應節點內屬性名稱public String getValue() {return value;}public void setValue(String value) {this.value = value;}}
package com.wgq.test09_xml;import com.thoughtworks.xstream.annotations.XStreamAlias;import com.thoughtworks.xstream.annotations.XStreamAsAttribute;@XStreamAlias("vientiance")public class Vientiance {@XStreamAsAttributeprivate String version;public String getVersion() {return version;}public void setVersion(String version) {this.version = version;}}
測試main方法
package com.wgq.test09_xml;import com.thoughtworks.xstream.XStream;import com.thoughtworks.xstream.io.xml.DomDriver;public class TestStream {public static void main(String[] args) {String reqXml = getXml();XStream xs = new XStream(new DomDriver()); xs.processAnnotations(new Class[]{AllnewstateRQ.class,Client.class,Sign.class,Vientiance.class});Object obj = xs.fromXML(reqXml);AllnewstateRQ allnewstateRQ = (AllnewstateRQ) obj;System.out.println(allnewstateRQ.getProtocol());System.out.println(allnewstateRQ.getClient().getArea());System.out.println(reqXml);}static String getXml(){StringBuilder str = new StringBuilder();str.append("").append("<config>").append("<client type=\"8888\" osversion=\"9999\" version=\"123\" oemtag=\"5555\" area=\"areacode\" />").append("<protocol>1.10</protocol>").append("<sign value=\"asdfasdf\" />").append("<vientiance version=\"version\" />").append("</config>");return str.toString();}}
輸出結果:
1.10areacode<config><client type="8888" osversion="9999" version="123" oemtag="5555" area="areacode" /><protocol>1.10</protocol><sign value="asdfasdf" /><vientiance version="version" /></config>
感謝 博主德莫羅 的 http://chen-rojer-gmail-com.iteye.com/blog/1435254 範例,
我參考 德莫羅 的寫的這個 單元測試,將XML 轉換成 指定的Object類的小例子.