稍微复杂一点的应用实例,idf项目所用的xml更为复杂头晕中
Doc.betwixt
Resource.betwixt
测试代码
输出结果
java 代码
- package betwixt;
- /**
- *
- */
- /**
- * @author huyunan
- *
- *
- * 2007 aspire
- */
- public class Music
- {
- private String name;
- private String mp3File;
- public Music()
- {
- }
- /**
- * @return the name
- */
- public String getName()
- {
- return this.name;
- }
- /**
- * @param name
- * the name to set
- */
- public void setName(String name)
- {
- this.name = name;
- }
- /**
- * @return the mp3File
- */
- public String getMp3File()
- {
- return this.mp3File;
- }
- /**
- * @param mp3File
- * the mp3File to set
- */
- public void setMp3File(String mp3File)
- {
- this.mp3File = mp3File;
- }
- }
java 代码
- package betwixt;
- /**
- *
- */
- /**
- * @author huyunan
- *
- *
- * 2007 aspire
- */
- public class Doc
- {
- private String version;
- private String name;
- private String url;
- public Doc()
- {
- }
- /**
- * @return the name
- */
- public String getDocName()
- {
- return this.name;
- }
- /**
- * @param name
- * the name to set
- */
- public void setName(String name)
- {
- this.name = name;
- }
- /**
- * @return the url
- */
- public String getUrl()
- {
- return this.url;
- }
- /**
- * @param url
- * the url to set
- */
- public void setUrl(String url)
- {
- this.url = url;
- }
- /**
- * @return the version
- */
- public String getVersion()
- {
- return this.version;
- }
- /**
- * @param version
- * the version to set
- */
- public void setVersion(String version)
- {
- this.version = version;
- }
- }
Doc.betwixt
xml 代码
- <?xml version="1.0" encoding="UTF-8" ?>
- <info primitiveTypes="element">
- <element name="docResource">
- <attribute name="version" property="version" />
- <element name="contentUrl" property="url" />
- <addDefaults />
- </element>
- </info>
java 代码
- /**
- *
- */
- package betwixt;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- /**
- * @author huyunan
- *
- *
- * 2007 aspire
- */
- public class Resource
- {
- private String version;
- private String name;
- private String author;
- private Doc doc;
- private Music music;
- private List<Doc> docList;
- private List<Music> musicList;
- private HashMap<String, Doc> docMap;
- private int docsSize;
- private int musicsSize;
- public Resource()
- {
- docList = new ArrayList<Doc>();
- musicList = new ArrayList<Music>();
- docMap = new HashMap<String, Doc>();
- }
- public void addToDocMap(String key, Doc doc)
- {
- docMap.put(key, doc);
- }
- public Map getDocMap()
- {
- return docMap;
- }
- public void addToDocList(Doc doc)
- {
- docList.add(doc);
- docsSize = docList.size();
- }
- public void addToMusicList(Music music)
- {
- musicList.add(music);
- musicsSize = musicList.size();
- }
- /**
- * @return the docsSize
- */
- public int getDocsSize()
- {
- return this.docsSize;
- }
- /**
- * @param docsSize
- * the docsSize to set
- */
- public void setDocsSize(int docsSize)
- {
- this.docsSize = docsSize;
- }
- /**
- * @return the docList
- */
- public List<Doc> getDocList()
- {
- return this.docList;
- }
- /**
- * @return the name
- */
- public String getName()
- {
- return this.name;
- }
- /**
- * @param name
- * the name to set
- */
- public void setName(String name)
- {
- this.name = name;
- }
- /**
- * @return the author
- */
- public String getAuthor()
- {
- return this.author;
- }
- /**
- * @return the doc
- */
- public Doc getDoc()
- {
- return this.doc;
- }
- /**
- * @param doc
- * the doc to set
- */
- public void setDoc(Doc doc)
- {
- this.doc = doc;
- }
- /**
- * @param author
- * the author to set
- */
- public void setAuthor(String author)
- {
- this.author = author;
- }
- /**
- * @return the version
- */
- public String getVersion()
- {
- return this.version;
- }
- /**
- * @param version
- * the version to set
- */
- public void setVersion(String version)
- {
- this.version = version;
- }
- /**
- * @return the music
- */
- public Music getMusic()
- {
- return this.music;
- }
- /**
- * @param music
- * the music to set
- */
- public void setMusic(Music music)
- {
- this.music = music;
- }
- /**
- * @return the musicList
- */
- public List<Music> getMusicList()
- {
- return this.musicList;
- }
- /**
- * @return the musicsSize
- */
- public int getMusicsSize()
- {
- return this.musicsSize;
- }
- /**
- * @param musicsSize
- * the musicsSize to set
- */
- public void setMusicsSize(int musicsSize)
- {
- this.musicsSize = musicsSize;
- }
- }
Resource.betwixt
xml 代码
- <?xml version="1.0" encoding="UTF-8" ?>
- <info primitiveTypes="element">
- <element name="sync">
- <attribute name="version" property="version"/>
- <element name="resource">
- <!--
- <hide property="name" />
- -->
- <element name="musicRes" property="music"/>
- <element name="docList">
- <attribute name="docsSize" property="docsSize"/>
- <element name="doc" property="docList"/>
- </element>
- <element name="musicList">
- <attribute name="musicsSize" property="musicsSize"/>
- <element name="music" property="musicList"/>
- </element>
- <element name="docMap">
- <element name="doc" property="docMap"/>
- </element>
- <addDefaults/>
- </element>
- </element>
- </info>
测试代码
java 代码
- Doc doc = new Doc();
- doc.setName("doc_name");
- doc.setUrl("doc_url");
- doc.setVersion("1.0.0");
- Music music = new Music();
- music.setName("music_name");
- music.setMp3File("music_mp3file");
- Resource resource = new Resource();
- 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);
- //
- Writer outputWriter = new FileWriter("e:\\test.xml");
- BeanWriter beanWriter = new BeanWriter(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("\t");
- //
- beanWriter.writeXmlDeclaration("<?xml version=\'1.0\' ?>");
- beanWriter.write(resource);
- outputWriter.close();
输出结果
xml 代码
- <?xml version='1.0' ?>
- <sync version="1.0.0">
- <resource>
- <musicRes>
- <mp3File>music_mp3file</mp3File>
- <name>music_name</name>
- </musicRes>
- <docList docsSize="2">
- <doc version="1.0.0">
- <contentUrl>doc_url</contentUrl>
- <docName>doc_name</docName>
- </doc>
- <doc version="1.0.0">
- <contentUrl>doc_url</contentUrl>
- <docName>doc_name</docName>
- </doc>
- </docList>
- <musicList musicsSize="2">
- <music>
- <mp3File>music_mp3file</mp3File>
- <name>music_name</name<sp>