不需要產生dtd,無用配置,不需要產生輔助類,速度快。這就是xstream+xpp超強黃金組合。
xstream大家都知道啦,XML Pull Parser是一種高速的 解析xml檔案的方式,速度要比傳統方式快很多(發現pull式解析現在比較流行了)。下面我給出多種使用方法的例子。
1.最簡單的使用方法
因為這個太簡單,所以我從moogle的blog http://moogle.javaeye.com/blog/34661取下面的例子
1. public static void write() {
2. XStream sm = new XStream();
3. mytest t = new mytest();
4. t.setName("moogle");
5. t.setXb("男");
6. try {
7. FileOutputStream ops = new FileOutputStream(new File("C://111.xml"));
8. sm.toXML(t, ops);
9. ops.close();
10. } catch (Exception e) {
11. e.printStackTrace();
12. }
13. }
14. public static void read() {
15. XStream sm = new XStream(new DomDriver());
16. try {
17. FileInputStream ops = new FileInputStream(new File("C://111.xml"));
18. mytest t = (mytest)sm.fromXML(ops);
19. System.out.println(t.getName());
20. ops.close();
21. } catch (Exception e) {
22. e.printStackTrace();
23. }
24. }
產生 XML是
# <mytest>
# <name>asd</name>
# <xb>男</xb>
2.中等方法(需要1.2版以上才有這個功能)
XStream stream = new XStream();
stream.alias("schema", SchemaObject.class);
stream.useAttributeFor("url", String.class);
stream.useAttributeFor("jdbcDriverClass", String.class);
stream.useAttributeFor("user", String.class);
stream.useAttributeFor("password", String.class);
FileOutputStream s = new FileOutputStream(file);
stream.toXML(theObject, s);
s.close();
alias和useAttributeFor是用作把 <com.hongsoft.model.SchemaObject>修改為<schema>
3.進階方法
XStream stream = new XStream();
stream.registerConverter(new SchemaXMLConvertor());
stream.alias("schema", SchemaObject.class);
FileInputStream s = new FileInputStream(file);
object = (SchemaObject) stream.fromXML(s);
s.close();
registerConverter可以實現把任何schema的XML和object互相轉換,這個其實已經否定了很多朋友說的
“xstream+xpp”功能不強的說法。SchemaXMLConvertor樣本如下:
public void marshal(Object arg0, HierarchicalStreamWriter writer,
MarshallingContext arg2) {
SchemaObject schema=(SchemaObject)arg0;
writer.startNode(SchemaObject.TABLE);
writer.addAttribute(SchemaObject.TABLE_NAME, iTable.getName());
//line
List<SchemaObject.TableObject.LineObject> lines=iTable.getLines();
for(int j=0;j<lines.size();j++)
{
SchemaObject.TableObject.LineObject jLine=lines.get(j);
writer.startNode(SchemaObject.LINE);
//column
Map<String,String> columns=jLine.getColumns();
Iterator ite=columns.keySet().iterator();
while(ite.hasNext())
{
String columnName=ite.next().toString();
writer.addAttribute(columnName, columns.get(columnName));
}
writer.endNode();
}
writer.endNode();
writer.underlyingWriter();
}
public Object unmarshal(HierarchicalStreamReader reader,
UnmarshallingContext arg1) {
SchemaObject schema=new SchemaObject();
schema.setJdbcDriverClass(reader.getAttribute(SchemaObject.JDBC_DRIVER_CLASS));
schema.setUrl(reader.getAttribute(SchemaObject.URL));
schema.setUser(reader.getAttribute(SchemaObject.USER));
schema.setPassword(reader.getAttribute(SchemaObject.PASSWORD));
List<TableObject> tables = new ArrayList<TableObject>();
while(reader.hasMoreChildren()) {
reader.moveDown();
SchemaObject.TableObject table=new SchemaObject().new TableObject();
table.setName(reader.getAttribute(SchemaObject.TABLE_NAME));
tables.add(table);
reader.moveUp();
}
schema.setTables(tables);
return schema;
}
public boolean canConvert(Class arg0) {
return arg0.equals(SchemaObject.class);
}
說明:1、XStream不要求Java類的屬性必須有getter、setter方法,因此省略。 2、設定java成員屬性別名 xStream.aliasField("Investor-List",MainBody.class,"investorList"); outXML(3,xStream,mainBody);運行結果會產生: <Investor-List> 3、將java成員映射為xml元素的一個屬性 //將name成員作為屬性添加到Investor對應xml節點裡 xStream.useAttributeFor(Investor.class,"name"); 運行結果會產生: <Investor name="hahhah"> 下面這兩句是相關聯的: xStream.aliasAttribute(Investor.class,"name","GDXM"); xStream.useAttributeFor(Investor.class,"name"); 意思是先為java成員定義個別名,然後在將別名應用於xml屬性上。 運行結果會產生: <Investor GDXM="hahhah"> 這些問題雖然解決了,但又發現了新的問題:xml轉java時,當java中沒有定義xml元素節點時,這時候會拋出異常。也許通過XStream本身的API可以解決,也許是XStream本身所不能處理的問題,時間有限,也沒來得及深究。有知道的朋友,還望留言。 4、再進行xml到java 的轉換過程中,XStream對象別名可以定義很多,涵蓋的類的範圍超過xml所能表達的範圍,這個也沒有關係,XStream還是會忠實地將xml還原為對象。但是如果xml範圍大於XStream所涵蓋類的範圍,那麼轉換過程會出錯。比如,要將一個Investor節點轉換為ManiBody對象,肯定會出錯。