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灯, 水草

请输入看板名称,例如:WOW站内搜寻

TOP