Betwixt使用笔记——一个模板文件搞定复杂结构的Bean
对于复杂结构的beans,是否能够仅使用一个完整的.betwixt模版文件,而不是多个呢?
例如,对于如下的bean:
public class Person{ public String getName(){} public void setName(String name){} public Pocket getPocket(){} public void setPocket(Pocket pock){} } public class Pocket{ public int getObjNum(){} …… public String[] getObjects(){} …… }
<person> <name>xiaoxiao</name> <pocket> <number>2</number> <objects> <object>000001</object> <object>000002</object> </objects> </pocket> </person>
//读取配置文件并加载 InputSource ins = new InputSource(new FileReader("D:\\workspace\\translate_demo\\cfg\\Person.betwixt")); beanWriter.getXMLIntrospector().register(ins); beanWriter.write(person);
<?xml version="1.0"?> <betwixt-config> <!--name of the class to map --> <class name="com.translate.demo.Person"> <!-- standard definitions (same as in standard .betwixt file) --> <element name="person"> <element name="name" property="name"/> <element name="ych-pocket"> <element property="pocket"/> </element> </element> </class> <class name="com.translate.demo.Pocket"> <element name="pocket"> <element name="number" property="objNum"/> <element name="objects"> <element name="object" property="objectName"/> </element> </element> </class> </betwixt-config>
<element property="pocket"/>
<class name="com.translate.demo.Pocket"> <element name="pocket"> ……
<element name="pocket_hello" property="pocket"/> …… <class name="com.translate.demo.Pocket"> <element name="pocket"> ……
<person> <name>xiaoxiao</name> <pocket_hello> <number>2</number> <objects> <object>000001</object> <object>000002</object> </objects> </pocket_hello> </person>