使用Betwixt将javaBean生成xml 之二

 
当有一对多的关系时,生成 xml
package betwixt;
 
import java.util.ArrayList;
import java.util.List;
 
public class NoteBean {
 
    private String toWho ;
 
    private String fromWho ;
 
    private String title ;
 
    private String note ;
   
    private List<User>   details = new ArrayList <User>();
   
    …….
 
 
public class User {
    private String name ;
    private String password ;
    /**
      * @return the name
      */
    public String getName() {
       return name ;
    }
 
    /**
      * @param name the name to set
      */
    public void setName(String name) {
       this . name = name;
    }
 
    public User(String name) {
       this . name = name;
    }
 
    /**
      * @return the password
      */
    public String getPassword() {
       return password ;
    }
 
    /**
      * @param password the password to set
      */
    public void setPassword(String password) {
       this . password = password;
    }
   
}
 
 
UserBean.betwixt
<info primitiveTypes="element">
 <element name="note">
     <element name="detailsaaa" >    // name 表示生成多个元素时父结点名
         <element name="useraa" property="details"/>   //name 子结点名, property UserBean 中的属性
     </element>
     <addDefaults />
 </element>
</info>
 
User.betwixt
<info primitiveTypes="element">
 <element name="note">
     <element name="subname" property="name" />
     <element name="password" property="password" />
     <addDefaults />
 </element>
</info>
 
测试:
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.getXMLIntrospector().getConfiguration().setElementNameMapper(new org.apache.commons.betwixt.strategy.HyphenatedNameMapper());
       
        beanWriter.getBindingConfiguration().setMapIDs( false );
      
        beanWriter.enablePrettyPrint();  // 启用缩进格式 .
 
        beanWriter.setEndTagForEmptyElement( true );   //true , 当为元素时 ,<aa></aa>, 不是 <aa/>
       
        beanWriter.setWriteEmptyElements( false );    // false , 表示如果是空元素 , 就不显示 . 比如如果 user 中的 password 为空,就不显示
       // beanWriter.setIndent("/t");
 
        beanWriter.writeXmlDeclaration( "" );
        NoteBean test= new NoteBean();
        test.setToWho( "fdfdf" );
        test.setFromWho( "frof" );
        test.setTitle( "tesdd" );
        test.setNote( "fdfddf" );
       
       
        List<User> list= new ArrayList <User>();
        list.add( new User( "aaa" ));
        list.add( new User( "bbb" ));
        list.add( new User( "cccc" ));
        test.setDetails(list);
        // 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();
    }
}
最终生成xml:
<?xml version='1.0' ?>
  <note>
    <detailsaaa>
      <useraa>
        <subname>aaa</subname>
      </useraa>
      <useraa>
        <subname>bbb</subname>
      </useraa>
      <useraa>
        <subname>cccc</subname>
      </useraa>
    </detailsaaa>
    <note>fdfddf</note>
    <title>tesdd</title>
    <toWho>fdfdf</toWho>
  </note>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值