本文記錄一下使用xstream這個(gè)api的注解特性對(duì)Java對(duì)象與XML字符串相互轉(zhuǎn)換的一些代碼示例。
我們很多人都處理過(guò)XML文件,也有很多非常成熟的第三方開源軟件。如:jdom、dom4j等。雖然他們的功能非常強(qiáng)大,但在使用上還是有點(diǎn)不那么習(xí)慣。對(duì)于格式比較固定的XML文檔,它的結(jié)構(gòu)沒(méi)有變化或是很少變化,這時(shí)將它轉(zhuǎn)換成我們熟悉的Java對(duì)象來(lái)操作的話,會(huì)使工作變得更容易一些,而xstream正好可以滿足這一點(diǎn)。
本文所用xstream的版本為:1.4.7
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.7</version>
</dependency>
還是以之前的book XML為例,先上代碼。
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
@XStreamAlias("book")
public class Book {
// 別名注解,這個(gè)別名就是XML文檔中的元素名,Java的屬性名不一定要與別名一致
@XStreamAlias("name")
private String name;
@XStreamAlias("author")
private String author;
// 屬性注解,此price就是book的屬性,在XML中顯示為:<book price="108">
@XStreamAsAttribute()
@XStreamAlias("price")
private String price;
省略get和set方法
}
import java.util.List;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
@XStreamAlias("books")
public class Books {
// 隱式集合,加上這個(gè)注解可以去掉book集合最外面的<list></list>這樣的標(biāo)記
@XStreamImplicit
private List<Book> list;
省略get和set方法
}
import java.util.List;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
public class XStreamHandle {
private static final String xmlString = "<books><book price=\"108\"><name>Java編程思想</name><author>Bruce Eckel</author></book><book price=\"52\"><name>Effective Java</name><author>Joshua Bloch</author></book><book price=\"118\"><name>Java 7入門經(jīng)典</name><author>Ivor Horton</author></book></books>";
public static String toXml(Object obj) {
XStream xstream = new XStream(new DomDriver("utf8"));
xstream.processAnnotations(obj.getClass()); // 識(shí)別obj類中的注解
/*
// 以壓縮的方式輸出XML
StringWriter sw = new StringWriter();
xstream.marshal(obj, new CompactWriter(sw));
return sw.toString();
*/
// 以格式化的方式輸出XML
return xstream.toXML(obj);
}
public static <T> T toBean(String xmlStr, Class<T> cls) {
XStream xstream = new XStream(new DomDriver());
xstream.processAnnotations(cls);
@SuppressWarnings("unchecked")
T t = (T) xstream.fromXML(xmlStr);
return t;
}
public static void main(String[] args) {
Books books = toBean(xmlString, Books.class);
List<Book> list = books.getList();
for(Book book : list) {
System.out.println("name=" + book.getName() + "\tauthor=" + book.getAuthor()
+ "\tprice=" + book.getPrice());
}
System.out.println(toXml(books));
}
}
除了上面示例中用的注解,xstream還有下面幾種注解也經(jīng)常用到。
@XstreamOmitField 忽略字段 這相當(dāng)于設(shè)置某些字段為臨時(shí)屬性,在轉(zhuǎn)換中不再起作用。 @XStreamConverter(XXX.class) 轉(zhuǎn)換器 XXX.class是一個(gè)實(shí)現(xiàn)了com.thoughtworks.xstream.converters.Converter接口的轉(zhuǎn)換器,對(duì)某些類型的值進(jìn)行轉(zhuǎn)換,比如布爾值類型的true或false,如果不加轉(zhuǎn)換器,默認(rèn)生成的值就是true或false。xstream自帶了BooleanConverter轉(zhuǎn)換器,可以將默認(rèn)值轉(zhuǎn)換成需要的文本值,如果xstream沒(méi)有需要的轉(zhuǎn)換器就得自己實(shí)現(xiàn)Converter接口來(lái)自定義轉(zhuǎn)換器。
根據(jù)大象的經(jīng)驗(yàn),為了少給自己找麻煩,比如避免使用轉(zhuǎn)換器,最好將與XML元素或?qū)傩詫?duì)應(yīng)的Java對(duì)象屬性都設(shè)置成String類型,當(dāng)然列表還是要定義成List類型的。只要不是特別奇葩,一般情況下,示例部分就能滿足絕大部分的需求。 本文為菠蘿大象原創(chuàng),如要轉(zhuǎn)載請(qǐng)注明出處。http://www.blogjava.net/bolo
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。