前端之家收集整理的这篇文章主要介绍了
Simple XML,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
今天介绍另一个Java Bean<->XML 之间序列化和反序列化的轻量级工具:Simple
官网:http://simple.sourceforge.net/home.php
截止目前最新版本(附近可下载):http://simple.sourceforge.net/download.php
特点:
- jar lib文件只有360K左右的大小
- 它的使用不需要依赖于其他 JAR 文件
- 通过注解的方式,灵活方便
下面将分节详细介绍Simple的特点和使用方法:
- [一]、简单bean的序列化和反序列化
- [二]、自定义节点名称
- [三]、嵌套对象
- [四]、可选的非强制性的元素或属性
- [五]、List<Object>处理
- [六]、inline 参数用法
- [七]、构造函数的注解处理
[一]、简单bean的序列化和反序列化
1.java bean
- packagemichael.serialization.simplexml;
-
- importjava.util.Date;
- importorg.simpleframework.xml.Attribute;
- importorg.simpleframework.xml.Element;
- importorg.simpleframework.xml.Root;
-
- *
- *@bloghttp://sjsky.iteye.com
- *@authorMichael
- */
- @Root
- publicclassMyTestVo{
- @Element
- privateStringuserName;
- @Attribute
- privateStringwife;
- privateStringrealName;
- privateDatebornDate;
- privateDoubleheight;
- publicStringtoString(){
- return@H_502_159@"MyTestVo:[userName="+userName+@H_502_159@",wife="+wife
- +@H_502_159@",realName="+realName+@H_502_159@",height="+height
- +bornDate+@H_502_159@"]";
- }
- //省略setget等方法
- ......
- }
2.序列化
static
voidmain(String[]args)
throwsException{
Stringxmlpath=@H_502_159@"d:/test/michael/simple_testvo.xml";
MyTestVovo=newMyTestVo();
vo.setUserName(@H_502_159@"michael");
vo.setRealName(@H_502_159@"大大");
vo.setWife(@H_502_159@"小小");
vo.setHeight(173.3d);
vo.setBornDate(newDate());
try{
Serializerserializer=newPersister();
Fileresult=newFile(xmlpath);
serializer.write(vo,result);
}catch(Exceptione){
e.printStackTrace();
序列化成功生成的simple_testvo.xml文件如下:
<myTestVowife="小小"realName="大大">
- userName>michael</bornDate>2011-09-2817:39:59.432CSTheight>173.3myTestVo>
ps:注解可以把Java的属性序列化时指定为属性或者节点元素
3.反序列化
把上述生成的XML文件反序列化成Java bean测试代码:
Filesource= MyTestVovo=serializer.read(MyTestVo.class,source);
System.out.println(vo);
如果XML中包括中文字符有可能反序列化时会报错,以utf-8的编码读取XML文件即可,故修改代码如下:
*@paramargs
*@throwsException
InputStreamReaderis=newInputStreamReader(newFileInputStream(
xmlpath),@H_502_159@"utf-8");
PropertyListparseVo=serializer.read(PropertyList. System.out.println(parseVo);
运行反序列化,打印Java bean信息如下:
MyTestVo : [ userName = michael,wife = 小小小,realName = 大大,height = 173.3,bornDate = Wed Sep 28 17:39:59 CST 2011 ]
[二]、自定义节点名称
1.java bean
@Root(name=@H_
502_159@"MyTest")
@Attribute(name=@H_502_159@"MyWife")
@Element(name=@H_502_159@"born")
@Override
//setget......
2.序列化
序列化后生成的simple_testvo.xml文件如下:
MyTestMyWife="小小"realName="大大"born>2011-09-2821:47:37.455CSTMyTest 可以和之前的序列化XML文件对比下,看看区别在哪里。
运行反序列化程序后的打印结果如下:
[三]、嵌套对象
classConfigurationVo{
privateServerVoserver;
privateintid;
publicServerVogetServer(){
returnserver;
intgetId(){
returnid;
voidsetServer(ServerVopServer){
server=pServer;
voidsetId(intpId){
id=pId;
classServerVo{
intport;
privateStringhost;
privateSecurityVosecurity;
intgetPort(){
returnport;
publicStringgetHost(){
returnhost;
publicSecurityVogetSecurity(){
returnsecurity;
voidsetPort(intpPort){
port=pPort;
voidsetHost(StringpHost){
host=pHost;
voidsetSecurity(SecurityVopSecurity){
security=pSecurity;
}
classSecurityVo{
booleanssl;
privateStringkeyStore;
booleanisSsl(){
returnssl;
publicStringgetKeyStore(){
returnkeyStore;
voidsetSsl(booleanpSsl){
ssl=pSsl;
voidsetKeyStore(StringpKeyStore){
keyStore=pKeyStore;
SecurityVosecurity=newSecurityVo();
security.setSsl(true);
security.setKeyStore(@H_502_159@"Michael");
ServerVoserver=newServerVo();
server.setHost(@H_502_159@"sjsky.iteye.com");
server.setPort(8088);
server.setSecurity(security);
ConfigurationVoconfig=newConfigurationVo();
config.setId(10000);
config.setServer(server);
FilexmlFile= serializer.write(config,xmlFile);
运行上述方法,序列化生成的XML文件如下:
configurationVoid="10000"serverport="8088"host>sjsky.iteye.comsecurityssl="true"keyStore>MichaelsecurityserverconfigurationVo>
3.反序列化的方法和之前的一致,自己可以测试下结果是否正确。
[四]、可选的非强制性的元素或属性
//不是每个人都有妻子的吼吼
@Attribute(required=false)
//不想泄露年龄噢
@Element(required=//省略settergetter方法
2.序列化
运行序列化程序后
生成的XML
文件如下:
myTestVorealName="大大" 运行反序列化程序后打印结果如下:
[五]、List<Object>处理
importjava.io.FileInputStream;
importjava.io.InputStreamReader;
importjava.util.ArrayList;
importjava.util.List;
importorg.simpleframework.xml.ElementList;
importorg.simpleframework.xml.Serializer;
importorg.simpleframework.xml.core.Persister;
classPropertyList{
@ElementList
privateList<EntryVo>list;
privateStringname;
publicList<EntryVo>getList(){
returnlist;
publicStringgetName(){
returnname;
voidsetList(List<EntryVo>pList){
list=pList;
voidsetName(StringpName){
name=pName;
return@H_502_159@"PropertyList:[name="+name+@H_502_159@",EntryVolistsize="
+list.size()+@H_502_159@"].";
classEntryVo{
privateStringvalue;
publicStringgetValue(){
returnvalue;
voidsetValue(StringpValue){
value=pValue;
PropertyListvo=initBean();
newFile(xmlpath));
staticPropertyListinitBean(){
PropertyListvo=newPropertyList();
vo.setName(@H_502_159@"WifeList");
List<EntryVo>subList=newArrayList<EntryVo>();
EntryVosubvo=newEntryVo();
subvo.setName(@H_502_159@"A");
subvo.setValue(@H_502_159@"福晋");
subList.add(subvo);
subvo= subvo.setName(@H_502_159@"B");
subvo.setValue(@H_502_159@"侧福晋");
subvo.setName(@H_502_159@"C");
subvo.setValue(@H_502_159@"小三");
subvo.setName(@H_502_159@"D");
subvo.setValue(@H_502_159@"二奶");
vo.setList(subList);
returnvo;
运行序列化程序后生成的XML文件如下:
propertyListname="WifeList"listclass="java.util.ArrayList"entryVoname="A"value>福晋entryVoentryVoname="B">侧福晋entryVoname="C">小三entryVoname="D">二奶listpropertyList3.反序列化,运行结果打印对象信息如下:
PropertyList : [ name = Wife List,EntryVo list size = 4 ] .
4.修改注解@ElementList的参数
@ElementList(name=@H_502_159@"WifeList",entry=@H_502_159@"wife")
privateList<EntryVo>list;
序列化后生成的XML文件如下:
WifeListclass="java.util.ArrayList"wifename="A"wifewifename="B"wifename="C"wifename="D"WifeList 注意XML文件的变化。
[六]、inline 参数用法
以上节中得bean为基础修改注解如下:
Highlighter" id="" style="font-family:Monaco,entry=@H_
502_159@"wife",inline=
true)
2.序列化后生成的XML文件如下:
<propertyListname=@H_502_159@"WifeList">
<wifename=@H_502_159@"A">
<value>福晋</value>
</wife>
<wifename=@H_502_159@"B">
<value>侧福晋</value>
<wifename=@H_502_159@"C">
<value>小三</value>
<wifename=@H_502_159@"D">
<value>二奶</value>
</propertyList>
和上节生成的文件相比,XML结构少了一个层次。
[七]、构造函数的注解处理
publicEntryVo(@Attribute(name=@H_502_159@"name")
Stringname,@Element(name=@H_502_159@"value")
Stringvalue){
this.name=name;
this.value=value;
return@H_502_159@"EntryVo:[name="+name+@H_502_159@",value="+value+@H_502_159@"].";
生成的XML文件如下:
<entryVo name="blog">
<value>http://sjsky.iteye.com</value>
</entryVo>
反序列化生成的bean的信息打印如下:
EntryVo : [ name = blog,value = http://sjsky.iteye.com ].
ps:如果java bean有参数的构函数,需要在构造函数的参数前也加上相应的注解,否则在反序列化时会出错。
本文连接:http://sjsky.iteye.com/blog/1182057