使用Betwixt将javaBean生成xml [转]


http://blog.csdn.net/pengchua/article/details/1955405

http://blog.csdn.net/pengchua/article/details/2410491

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间的相互映射。)

http://commons.apache.org/betwixt/apidocs/index.html
将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;

publicclass NoteBean {

    private String toWho;

    private String fromWho;

    private String title;

    private String note;

    public NoteBean() {
    }

    public String getFromWho() {
       returnfromWho;
    }

    publicvoid setFromWho(String fromWho) {
       this.fromWho = fromWho;
    }

    public String getNote() {
       returnnote;
    }

    publicvoid setNote(String note) {
       this.note = note;
    }

    public String getTitle() {
       returntitle;
    }

    publicvoid setTitle(String title) {
       this.title = title;
    }

    public String getToWho() {
       returntoWho;
    }

    publicvoid 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;
publicclass WriteExampleApp {

    /**
     *Createanexamplebeanandthenconvertittoxml.
     */
    publicstaticfinalvoid 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();
    }
}

你可能感兴趣的:(javabean)