java 板


[功能] 1. 把 java bean 轉成 xml 2. 把 xml 轉成 java bean [相依的 component] 大致上同 Digester 的相依性,再加上自己 (如果你只做 bean to xml,大概是用不到 Digester) [程序] 1. 產生 BeanWriter/BeanReader BeanWriter beanWriter = new BeanWriter(outputWriter); BeanReader beanReader = new BeanReader(); 2. Configure BeanWriter/BeanReader beanWriter.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false); beanWriter.getBindingConfiguration().setMapIDs(false); beanWriter.enablePrettyPrint(); beanReader.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false); beanReader.getBindingConfiguration().setMapIDs(false); 3. 執行 write 動作 / 執行 read 動作 beanWriter.write("room", new CustomBean()); CustomBean cbean = (CustomBean) beanReader.parse(xmlReader); *. ===================================================.* read 動作之前要先 register bean class beanReader.registerBeanClass("cbean", CustomBean.class); 參數 1 是 root element name *. ===================================================.* 4. xml 建立完成 / java instance 建立完成 ======================================================================||||||| [範例] 俺假設要建立的是房客與客戶的資料,房間有編號與容量,客人有姓名和身份證 字號。房間以 Collection 持有客人的資訊。 import java.util.Collection; /** * Room bean contain room and customs information * @author qrtt1 * */ public class Room { private Collection customs; private Integer number; private Integer capacity; public Room() { } /** * @param number the number of this room * @param capacity how many custom this room can live */ public Room(Integer number, Integer capacity) { assert (number != 0); assert (capacity != 0); this.number = number; this.capacity = capacity; } /** * @param number the number of this room * @param capacity how many custom this room can live * @param customs what are customs having lived in */ public Room(Integer number, Integer capacity, Collection customs) { this(number, capacity); assert (customs != null); this.customs = customs; } /** * @return the capacity of this room */ public Integer getCapacity() { return this.capacity; } /** * @param capacity set capacity for this room */ public void setCapacity(Integer capacity) { this.capacity = capacity; } /** * @return the customs which live in the room */ public Collection getCustoms() { return this.customs; } /** * @param customs set customs which live in the room */ public void setCustoms(Collection customs) { this.customs = customs; } /** * @return the room no. */ public Integer getNumber() { return this.number; } /** * @param number set room no. */ public void setNumber(Integer number) { this.number = number; } /** * @param c add new custom c * @return true if still has space for new custom */ public boolean addCustom(Custom c){ if( customs.size() < capacity){ return this.customs.add(c); } return false; } /** * @param c remove the custom c from this room * @return true if the custom in this room */ public boolean removeCustom(Custom c){ return customs.remove(c); } } /** * Custom bean store the information of a custom * @author qrtt1 * */ public class Custom { private String name; private String id; public Custom(){} /** * @param name custom's name * @param id custom's id */ public Custom(String name, String id) { assert (name != null && (!"".equals(name))); assert (id != null && (!"".equals(id))); this.name = name; this.id = id; } /** * @return custom's id */ public String getId() { return this.id; } /** * @param id custom's id */ public void setId(String id) { this.id = id; } /** * @return custom's name */ public String getName() { return this.name; } /** * @param name custom's name */ public void setName(String name) { this.name = name; } } 接著,為了方便起見,我們建立一個 TestData 類別 import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Collection; import java.util.Vector; public class TestData { /** * @return return out test data */ public static Room getData() { // room no. 103, capacity 8p Room m = new Room(103, 8); Collection<Custom> c = new Vector<Custom>(); c.add(new Custom("qrtt1", "A176537653")); c.add(new Custom("orz", "A2123426543")); c.add(new Custom("xd", "X123456271")); m.setCustoms(c); return m; } public static String getXMLData() { StringBuffer sbuff = new StringBuffer(); String tmp; try { FileReader reader = new FileReader("/home/qrtt1/room.xml"); BufferedReader breader = new BufferedReader(reader); tmp = breader.readLine(); while (tmp != null) { sbuff.append(tmp); tmp = breader.readLine(); } return sbuff.toString(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return ""; } } ==============================================================|||||| 讀寫則直接修改手冊裡的範例 import java.io.StringWriter; public class WriteExampleApp { /** * Create an example bean and then convert it to xml. */ public static final void main(String [] args) throws Exception { // Start by preparing the writer // We'll write to a string StringWriter outputWriter = new StringWriter(); // Betwixt just writes out the bean as a fragment // So if we want well-formed xml, we need to add the prolog outputWriter.write("<?xml version='1.0' ?>\n"); // Create a BeanWriter which writes to our prepared stream BeanWriter beanWriter = new BeanWriter(outputWriter); // Configure betwixt // For more details see java docs or later in the main documentation beanWriter.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false); beanWriter.getBindingConfiguration().setMapIDs(false); beanWriter.enablePrettyPrint(); // If the base element is not passed in, Betwixt will guess // But let's write example bean as base element 'person' //beanWriter.write("person", new PersonBean("John Smith", 21)); beanWriter.write("room", TestData.getData()); // Write to System.out // (We could have used the empty constructor for BeanWriter // but this way is more instructive) System.out.println(outputWriter.toString()); // Betwixt writes fragments not documents so does not automatically close // writers or streams. // This example will do no more writing so close the writer now. outputWriter.close(); } } import java.io.StringWriter; public class WriteExampleApp { /** * Create an example bean and then convert it to xml. */ public static final void main(String [] args) throws Exception { // Start by preparing the writer // We'll write to a string StringWriter outputWriter = new StringWriter(); // Betwixt just writes out the bean as a fragment // So if we want well-formed xml, we need to add the prolog outputWriter.write("<?xml version='1.0' ?>\n"); // Create a BeanWriter which writes to our prepared stream BeanWriter beanWriter = new BeanWriter(outputWriter); // Configure betwixt // For more details see java docs or later in the main documentation beanWriter.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false); beanWriter.getBindingConfiguration().setMapIDs(false); beanWriter.enablePrettyPrint(); // If the base element is not passed in, Betwixt will guess // But let's write example bean as base element 'person' //beanWriter.write("person", new PersonBean("John Smith", 21)); beanWriter.write("room", TestData.getData()); // Write to System.out // (We could have used the empty constructor for BeanWriter // but this way is more instructive) System.out.println(outputWriter.toString()); // Betwixt writes fragments not documents so does not automatically close // writers or streams. // This example will do no more writing so close the writer now. outputWriter.close(); } } ======================================================================= [心得] 1. 讀與寫的 configure 如果不同的話,會發生讀取的錯誤。 2. 請看倌自行 disable configure -> true 改成 false, false 改成 true 觀察結果。 比較能 make sense 3. 由於 1, 2 的體驗後,你會感到雖然很方便但仍不算實用, 因為 xml 總不是如你預期的樣子, 所以還需要看如何使用客製化設定檔 .betwixt -- 網頁版本 http://www.javaworld.com.tw/jute/post/view?bid=11&id=181066&sty=1&tpg=1&age=0 -- ※ Origin: SayYA 資訊站 <bbs.sayya.org> ◆ From: 163.26.34.20







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草
伺服器連線錯誤,造成您的不便還請多多包涵!
「贊助商連結」






like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:Boy-Girl站內搜尋

TOP