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 {...}