`
oklook249900241
  • 浏览: 27172 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

使用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();
    }
}

轉載來自:http://blog.csdn.net/pengchua/article/details/1955405

<!--EndFragment-->

分享到:
评论

相关推荐

    使用Betwixt将XML转为JAVA Bean(内有JAR包)

    它通过分析Java类的结构来生成XML,或者根据XML的结构来创建Java对象实例。在处理相同节点名的情况下,Betwixt允许我们通过属性名和类型来区分不同的节点。 以下是一个简单的Java Bean示例: ```java public class...

    betwixt互相转换JavaBean和XML(含代码和jar包)

    本资料用于JavaBean和XML之间相互转换,所用到的关于betwixt的包都在内。方法写在textXml中,由于默认的方法较为简单,且有局限性,所以本资料主要侧重从自定义的配置文件中转换xml或者生成javaBean.有兴趣的朋友...

    betwixt互相转换JavaBean和XML(含代码和jar包).rar

    本资料用于JavaBean和XML之间相互转换,所用到的关于betwixt的包都在内。方法写在textXml中,由于默认的方法较为简单,且有局限性,所以本资料主要侧重从自定义的配置文件中转换xml或者生成javaBean.有兴趣的朋友...

    commons-betwixt-0.5-src.zip_ObjectStringConvert_commons-betwixt_

    Betwixt的工作原理是通过分析JavaBean的公共属性来生成XML表示,同时也能够根据XML文档的结构实例化相应的Java对象。这种映射机制使得开发者无需手动编写大量的XML解析和序列化代码,降低了出错的可能性,也提高了...

    Java序列化技术的探讨.pdf

    然后,在运行时,调用框架中的基于反射的工具,并根据对象成员的类型、名称和值生成XML文档。通过定义类的映射文件,或使用注释自定义XML生成方式。 下面重点介绍JAX B和X Stream: (1) JAX B JAX B(Java ...

    commons.rar

    通过简单的注解或XML映射文件,Betwixt可以理解对象结构,并生成相应的XML表示,反之亦然。这种方式使得XML处理变得更加直观和易于维护。 除了这两个特定的组件,Apache Commons项目还包括许多其他模块,如IO、...

    apache web组件使用

    - **Jelly**:基于标签库的脚本语言,用于生成XML或Java代码。 - **JXPath**:使用XPath表达式在Java对象模型(如Map和JavaBean)中导航和修改数据。 3. **工具组件**: - **BeanUtils**:提供JavaBean属性的...

    jar包介绍大全

    - **DOM4J (dom4j.jar)**:DOM4J 是一个用于处理XML文档的Java库,它提供了简洁高效的API来解析和生成XML文档。 - **ODMG (odmg.jar)**:ODMG 是一个对象数据管理组织,它定义了一套标准来支持对象/关系映射。...

    commons包介绍1

    2. **Betwixt**: Betwixt 提供了将JavaBean映射到XML文档和反向映射的功能,使得XML数据与Java对象之间的转换变得更加简单。 3. **Chain**: Chain组件实现了责任链模式,允许构建复杂的处理流程,其中每个处理者...

Global site tag (gtag.js) - Google Analytics