使用Betwixt将javaBean生成xml

 
The Betwixt library provides an XML introspection mechanism for mapping beans to XML in a flexible way. It is implemented using an XMLIntrospector and XMLBeanInfo classes which are similar to the standard Introspector and BeanInfo from the Java Beans specification. (提供 XML JAVABEAN 间的相互映射。)
 
将javaBean生成Xml的步骤:
1.       要产生的 目标xml:
<?xml version='1.0' ?>
 <note>
    <to>fdfdf</to>
    <from name="frof"></from>
    <heading name="tesdd"></heading>
    <body name="fdfddf"></body>
 </note>
2.而原始javaBean的信息为:
package betwixt;
 
public class NoteBean {
 
    private String toWho ;
 
    private String fromWho ;
 
    private String title ;
 
    private String note ;
 
    public NoteBean() {
    }
 
    public String getFromWho() {
       return fromWho ;
    }
 
    public void setFromWho(String fromWho) {
       this . fromWho = fromWho;
    }
 
    public String getNote() {
       return note ;
    }
 
    public void setNote(String note) {
       this . note = note;
    }
 
    public String getTitle() {
       return title ;
    }
 
    public void setTitle(String title) {
       this . title = title;
    }
 
    public String getToWho() {
       return toWho ;
    }
 
    public void setToWho(String toWho) {
       this . toWho = toWho;
    }
 
}
 
2.       撰写 .betwixt ,文件名需和 bean 相同,故为 NoteBean. betwixt
<info primitiveTypes="element">
 <element name="note">
    <element name="to" property="toWho" />
      <element name="from">
        <attribute name="name" property="fromWho"/>
    </element>
        <element name="heading">
        <attribute name="name" property="title"/>
    </element>
        <element name="body">
        <attribute name="name" property="note"/>
    </element>
 <!--   <addDefaults />-->
 </element>
</info>
 
注意:  a. <element name="to" property="toWho" />
              表示 name 要显示的 xml 中的元素名 , property 表示对应 javaBean 中一个属性名,其值显示在 <></> 里面。
       b. <element name="from">
        <attribute name="name" property="fromWho"/>
    </element>
中间加: <attribute name="name" property="fromWho"/>
    表示会在在结点上加一个属性为 name ,其值显示在里面。
       c. <addDefaults /> 加上默认的结点,是以 javaBean 中的属性来显示。
3. 写测试代码:
import java.io.StringWriter;
import org.apache.commons.betwixt.io.BeanWriter;
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' ?>" );
       
        // 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();  // 启用缩进格式 .
 
        beanWriter.setEndTagForEmptyElement( true );
       // beanWriter.setIndent("/t");
 
        beanWriter.writeXmlDeclaration( "" );
        NoteBean test= new NoteBean();
        test.setToWho( "fdfdf" );
        test.setFromWho( "frof" );
        test.setTitle( "tesdd" );
        test.setNote( "fdfddf" );
       
        // If the base element is not passed in, Betwixt will guess
        // But let's write example bean as base element 'person'
        beanWriter.write( "note" , test);
       
        // 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
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值