Jakarta-common-betwixt Use Notes

Source: Internet
Author: User

To use it to convert JavaBean into XML and output.

Download Address: http://commons.apache.org/beanutils/

The code is as follows:

Javabean:

package demo.bean;

public class Music ...{

  private String name;

  private String mp3File;

  public String getName() ...{
    return name;
  }

  public void setName(String name) ...{
    this.name = name;
  }

  public String getMp3File() ...{
    return mp3File;
  }

  public void setMp3File(String mp3File) ...{
    this.mp3File = mp3File;
  }
}

Test code:

package demo;

Import java.beans.IntrospectionException;
Import Java.io.FileWriter;
Import java.io.IOException;
Import Java.io.Writer;

Import org.apache.commons.betwixt.io.BeanWriter;
Import org.xml.sax.SAXException;

Import Demo.bean.Music;

public class Betwixtdemo ... {

public static void Main (string[] args) throws IOException, Saxexception, introspectionexception ...     {

Music music = new Music ();
Music.setname ("Music_name");
Music.setmp3file ("Music_mp3file");
Writer outputwriter = new FileWriter ("E:\test.xml");
Beanwriter beanwriter = new Beanwriter (outputwriter);
Beanwriter.getxmlintrospector (). GetConfiguration (). Setattributesforprimitives (false);
Beanwriter.getbindingconfiguration (). Setmapids (false);
Beanwriter.enableprettyprint ();
Beanwriter.setendtagforemptyelement (TRUE);
Beanwriter.setindent ("");
Beanwriter.writexmldeclAration ("<?xml version=" 1.0 "encoding=" UTF-8 "?>");
Beanwriter.write ("MSG", music);
Outputwriter.close ();
}
}

You can specify namemapper to redefine case

Beanwriter.getxmlintrospector (). GetConfiguration (). Setelementnamemapper (New Org.apache.commons.betwixt.strategy.DecapitalizeNameMapper ());

which

Decapitalizenamemapper refers to the definition of lowercase

Capitalizenamemapper specified as Uppercase

Hyphenatednamemapper is specified as a connector such as Mp3file ==> mp3-file

You can set the case of the first letter by Hyphenatednamemapper.setuppercase (true)

You can also implement Namemapper to define your own realistic way.

such as: Class Itsanamemapper implements Namemapper {...}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.