Commons Betwixt 五步

本文介绍了一个使用Betwixt进行Java对象与XML文件之间的映射的应用案例。通过具体的代码示例展示了如何定义Java类,并配置XML映射文件来实现对象与XML元素之间的转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

稍微复杂一点的应用实例,idf项目所用的xml更为复杂头晕中
packagebetwixt;

/***//**
*
*/


/***//**
*
@authorhuyunan
*
*
*2007aspire
*/

publicclassMusic
...{

privateStringname;

privateStringmp3File;

publicMusic()
...{

}


/***//**
*
@returnthename
*/

publicStringgetName()
...{
returnthis.name;
}


/***//**
*
@paramname
*thenametoset
*/

publicvoidsetName(Stringname)
...{
this.name=name;
}


/***//**
*
@returnthemp3File
*/

publicStringgetMp3File()
...{
returnthis.mp3File;
}


/***//**
*
@parammp3File
*themp3Filetoset
*/

publicvoidsetMp3File(Stringmp3File)
...{
this.mp3File=mp3File;
}


}

packagebetwixt;

/***//**
*
*/

/***//**
*
@authorhuyunan
*
*
*2007aspire
*/
publicclassDoc
...{

privateStringversion;

privateStringname;

privateStringurl;

publicDoc()
...{

}


/***//**
*
@returnthename
*/

publicStringgetDocName()
...{
returnthis.name;
}


/***//**
*
@paramname
*thenametoset
*/

publicvoidsetName(Stringname)
...{
this.name=name;
}


/***//**
*
@returntheurl
*/

publicStringgetUrl()
...{
returnthis.url;
}


/***//**
*
@paramurl
*theurltoset
*/

publicvoidsetUrl(Stringurl)
...{
this.url=url;
}


/***//**
*
@returntheversion
*/

publicStringgetVersion()
...{
returnthis.version;
}


/***//**
*
@paramversion
*theversiontoset
*/

publicvoidsetVersion(Stringversion)
...{
this.version=version;
}


}

Doc.betwixt
<?xmlversion="1.0"encoding="UTF-8"?>
<infoprimitiveTypes="element">
<elementname="docResource">
<attributename="version"property="version"/>
<elementname="contentUrl"property="url"/>
<addDefaults/>
</element>
</info>

/***//**
*
*/

packagebetwixt;

importjava.util.ArrayList;
importjava.util.HashMap;
importjava.util.List;
importjava.util.Map;

/***//**
*
@authorhuyunan
*
*
*2007aspire
*/

publicclassResource
...{

privateStringversion;

privateStringname;

privateStringauthor;

privateDocdoc;

privateMusicmusic;

privateList<Doc>docList;

privateList<Music>musicList;

privateHashMap<String,Doc>docMap;

privateintdocsSize;

privateintmusicsSize;

publicResource()
...{
docList
=newArrayList<Doc>();
musicList
=newArrayList<Music>();
docMap
=newHashMap<String,Doc>();
}


publicvoidaddToDocMap(Stringkey,Docdoc)
...{
docMap.put(key,doc);
}


publicMapgetDocMap()
...{
returndocMap;
}


publicvoidaddToDocList(Docdoc)
...{
docList.add(doc);
docsSize
=docList.size();
}


publicvoidaddToMusicList(Musicmusic)
...{
musicList.add(music);
musicsSize
=musicList.size();
}


/***//**
*
@returnthedocsSize
*/

publicintgetDocsSize()
...{
returnthis.docsSize;
}


/***//**
*
@paramdocsSize
*thedocsSizetoset
*/

publicvoidsetDocsSize(intdocsSize)
...{
this.docsSize=docsSize;
}


/***//**
*
@returnthedocList
*/

publicList<Doc>getDocList()
...{
returnthis.docList;
}


/***//**
*
@returnthename
*/

publicStringgetName()
...{
returnthis.name;
}


/***//**
*
@paramname
*thenametoset
*/

publicvoidsetName(Stringname)
...{
this.name=name;
}


/***//**
*
@returntheauthor
*/

publicStringgetAuthor()
...{
returnthis.author;
}


/***//**
*
@returnthedoc
*/

publicDocgetDoc()
...{
returnthis.doc;
}


/***//**
*
@paramdoc
*thedoctoset
*/

publicvoidsetDoc(Docdoc)
...{
this.doc=doc;
}


/***//**
*
@paramauthor
*theauthortoset
*/

publicvoidsetAuthor(Stringauthor)
...{
this.author=author;
}


/***//**
*
@returntheversion
*/

publicStringgetVersion()
...{
returnthis.version;
}


/***//**
*
@paramversion
*theversiontoset
*/

publicvoidsetVersion(Stringversion)
...{
this.version=version;
}


/***//**
*
@returnthemusic
*/

publicMusicgetMusic()
...{
returnthis.music;
}


/***//**
*
@parammusic
*themusictoset
*/

publicvoidsetMusic(Musicmusic)
...{
this.music=music;
}


/***//**
*
@returnthemusicList
*/

publicList<Music>getMusicList()
...{
returnthis.musicList;
}


/***//**
*
@returnthemusicsSize
*/

publicintgetMusicsSize()
...{
returnthis.musicsSize;
}


/***//**
*
@parammusicsSize
*themusicsSizetoset
*/

publicvoidsetMusicsSize(intmusicsSize)
...{
this.musicsSize=musicsSize;
}


}

Resource.betwixt
<?xmlversion="1.0"encoding="UTF-8"?>
<infoprimitiveTypes="element">
<elementname="sync">
<attributename="version"property="version"/>
<elementname="resource">
<!--
<hideproperty="name"/>
-->
<elementname="musicRes"property="music"/>
<elementname="docList">
<attributename="docsSize"property="docsSize"/>
<elementname="doc"property="docList"/>
</element>
<elementname="musicList">
<attributename="musicsSize"property="musicsSize"/>
<elementname="music"property="musicList"/>
</element>
<elementname="docMap">
<elementname="doc"property="docMap"/>
</element>
<addDefaults/>
</element>
</element>
</info>

测试代码
Docdoc=newDoc();
doc.setName(
"doc_name");
doc.setUrl(
"doc_url");
doc.setVersion(
"1.0.0");
Musicmusic
=newMusic();
music.setName(
"music_name");
music.setMp3File(
"music_mp3file");
Resourceresource
=newResource();
resource.setVersion(
"1.0.0");
resource.setDoc(doc);
resource.setMusic(music);
resource.setName(
"resource_name");
resource.setAuthor(
"resource_author");
//
resource.addToDocList(doc);
resource.addToDocList(doc);
resource.addToMusicList(music);
resource.addToMusicList(music);
//
resource.addToDocMap("doc1",doc);
//resource.addToMap("mucis1",music);
//
WriteroutputWriter=newFileWriter("e:/test.xml");
BeanWriterbeanWriter
=newBeanWriter(outputWriter);

/***//**
*beanWriter.getXMLIntrospector().getConfiguration().setElementNameMapper(new
*org.apache.commons.betwixt.strategy.DecapitalizeNameMapper());
*beanWriter.getXMLIntrospector().getConfiguration().setElementNameMapper(new
*org.apache.commons.betwixt.strategy.CapitalizeNameMapper());
*beanWriter.getXMLIntrospector().getConfiguration().setElementNameMapper(new
*org.apache.commons.betwixt.strategy.HyphenatedNameMapper());
*/


//beanWriter.getXMLIntrospector().getConfiguration().setPluralStemmer(new
//ChinesePluralMapper());
beanWriter.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false);
beanWriter.getBindingConfiguration().setMapIDs(
false);
beanWriter.enablePrettyPrint();
beanWriter.setEndTagForEmptyElement(
true);
beanWriter.setIndent(
" ");
//
beanWriter.writeXmlDeclaration("<?xmlversion='1.0'?>");
beanWriter.write(resource);
outputWriter.close();

输出结果
<?xmlversion='1.0'?>
<syncversion="1.0.0">
<resource>
<musicRes>
<mp3File>music_mp3file</mp3File>
<name>music_name</name>
</musicRes>
<docListdocsSize="2">
<docversion="1.0.0">
<contentUrl>doc_url</contentUrl>
<docName>doc_name</docName>
</doc>
<docversion="1.0.0">
<contentUrl>doc_url</contentUrl>
<docName>doc_name</docName>
</doc>
</docList>
<musicListmusicsSize="2">
<music>
<mp3File>music_mp3file</mp3File>
<name>music_name</name>
</music>
<music>
<mp3File>music_mp3file</mp3File>
<name>music_name</name>
</music>
</musicList>
<docMap>
<doc>
<key>doc1</key>
<valueversion="1.0.0">
<contentUrl>doc_url</contentUrl>
<docName>doc_name</docName>
</value>
</doc>
</docMap>
<author>resource_author</author>
<docversion="1.0.0">
<contentUrl>doc_url</contentUrl>
<docName>doc_name</docName>
</doc>
<name>resource_name</name>
</resource>
</sync>




















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值