package betwixt;
import java.io.StringWriter;
import org.apache.commons.betwixt.io.BeanWriter;
/**
* @author Administrator yzd
*
*/
public class JavaBean2XML {
/**
*
* @param obj 被转化的bean
* @param element 设置根节点的别名
* @return String 格式良好的xml字符串
* @throws Exception
*/
public static String bean2xml(Object obj, String element) throws Exception {
StringWriter stringWriter = new StringWriter();
BeanWriter beanWriter = new BeanWriter(stringWriter);
beanWriter.writeXmlDeclaration("<?xml version='1.0' ?>");
// 是否禁止id属性(id属性是betwixt自动添加的)
beanWriter.getBindingConfiguration().setMapIDs(false);
// 当遇到ArrayList或者Map时做一个包装(默认true)
// beanWriter.getXMLIntrospector().getConfiguration().setWrapCollectionsInElement(true);
// 是否以缩进方式显示
beanWriter.enablePrettyPrint();
// true:元素为空时,显示为<element></element> ,false:显示<element/>
beanWriter.setEndTagForEmptyElement(true);
// 如果是null就写一个空值
beanWriter.setWriteEmptyElements(true);
// 开始转换
//beanWriter.write(obj);
beanWriter.write(element, obj);
beanWriter.close();
return stringWriter.toString();
}
}
package betwixt;
import java.io.StringReader;
import org.apache.commons.betwixt.io.BeanReader;
/**
* @author Administrator yzd
*
*/
public class XML2JavaBean {
/**
*
* @param xmlString xml字符串
* @param obj 要转化为哪个对象
* rootName 根节点名字
* @return
*/
public Object xml_java(String xmlString,Object obj,String rootName) {
//创建读取xml文件的流
StringReader xmlReader=new StringReader(xmlString);
//创建一个BeanReader实例,相当于一个转化器
BeanReader reader = new BeanReader();
// 注册要转换的类,并指定根节点名称
try {
//reader.registerBeanClass(rootName, obj.getClass());
reader.registerBeanClass(obj.getClass());
// 开始解析,并生成对象
obj= reader.parse(xmlReader);
// 检查转换得到的person是否包含文件中的值
} catch (Exception e) {
e.printStackTrace();
}
return obj;
}
//读取配置文件并加载
InputSource ins = new InputSource(new FileReader("D:\\workspace\\translate_demo\\cfg\\Person.betwixt"));
beanWriter.getXMLIntrospector().register(ins);
beanWriter.write(person);
betwixt文件类型要放在与javaBean同一个包下,就不用手动加载,组件会自动去当前Bean的目录下去找betwixt文件
在使用betwixt根据Bean生成xml文件时,Betwixt默认识别的xml模版文件是以bean的名称为主文件名的".betwixt"文件。
比如:
person.class
person.betwixt
如果我希望能够重新指定其他文件作为模版文件的话,该如何操作呢?
比如我希望使用hello.betwixt作为person.class的模板文件,
BeanWriter提供了如下方法:
public void write(Object bean, InputSource source)
InputSource ins = new InputSource(new FileReader("D:\\workspace\\translate_demo\\cfg\\hello.betwixt"));
beanWriter.write(person,ins);
public class Father {
private String fathername;
private Children c;
//省略getter/setter
}
public class Children {
private String childrenName;
private int id;
//省略getter/setter
}
<info primitiveTypes="element">
<element name="FatherElement">
<element name="lh" property="c" class="betwixt.domain.Children"/>
<addDefaults/>
<element name="string" property="fathername"/>
</element>
</info>
<info primitiveTypes="element">
<element>
<element name="cName" property="childrenName" />
<element name="cId" property="id"/>
</element>
</info>
-------------------------------------------------------------------
<?xml version='1.0' ?>
<bebe>
<lh>
<cName>小老虎</cName>
<cId>0</cId>
</lh>
<fathername>老虎</fathername>
<string>老虎</string>
</bebe>ng>老虎</string>
</bebe>
-------------------------------------------------------------------
<info primitiveTypes="element">
<element name="FatherElement">
<element name="lh" property="c" class="betwixt.domain.Children"/>
<addDefaults/>
<element name="string" property="fathername"/>
</element>
</info>
-------------------------------------------------------------------
<info primitiveTypes="element">
<element name="FatherElement">
<element name="lh" property="c" class="betwixt.domain.Children"/>
<addDefaults/>
<element name="string" property="fathername"/>
</element>
</info>
commons-collections-3.2.jar
commons-logging-1.0.4.jar
commons-beanutils-1.8.0-BETA.jar
http://blog.csdn.net/gaolingep/archive/2009/07/22/4369962.aspx