`
phantomhu
  • 浏览: 19909 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

Commons Betwixt 五步

阅读更多
稍微复杂一点的应用实例,idf项目所用的xml更为复杂头晕中
java 代码
 
  1. package betwixt;  
  2.   
  3. /** 
  4.  *  
  5.  */  
  6.   
  7. /** 
  8.  * @author huyunan 
  9.  *  
  10.  *  
  11.  * 2007 aspire 
  12.  */  
  13. public class Music  
  14. {  
  15.   
  16.     private String name;  
  17.   
  18.     private String mp3File;  
  19.   
  20.     public Music()  
  21.     {  
  22.   
  23.     }  
  24.   
  25.     /** 
  26.      * @return the name 
  27.      */  
  28.     public String getName()  
  29.     {  
  30.         return this.name;  
  31.     }  
  32.   
  33.     /** 
  34.      * @param name 
  35.      *            the name to set 
  36.      */  
  37.     public void setName(String name)  
  38.     {  
  39.         this.name = name;  
  40.     }  
  41.   
  42.     /** 
  43.      * @return the mp3File 
  44.      */  
  45.     public String getMp3File()  
  46.     {  
  47.         return this.mp3File;  
  48.     }  
  49.   
  50.     /** 
  51.      * @param mp3File 
  52.      *            the mp3File to set 
  53.      */  
  54.     public void setMp3File(String mp3File)  
  55.     {  
  56.         this.mp3File = mp3File;  
  57.     }  
  58.   
  59. }  

java 代码
 
  1. package betwixt;  
  2.   
  3. /** 
  4.  *  
  5.  */  
  6.   
  7. /** 
  8.  * @author huyunan 
  9.  *  
  10.  *  
  11.  * 2007 aspire 
  12.  */  
  13. public class Doc  
  14. {  
  15.   
  16.     private String version;  
  17.   
  18.     private String name;  
  19.   
  20.     private String url;  
  21.   
  22.     public Doc()  
  23.     {  
  24.   
  25.     }  
  26.   
  27.     /** 
  28.      * @return the name 
  29.      */  
  30.     public String getDocName()  
  31.     {  
  32.         return this.name;  
  33.     }  
  34.   
  35.     /** 
  36.      * @param name 
  37.      *            the name to set 
  38.      */  
  39.     public void setName(String name)  
  40.     {  
  41.         this.name = name;  
  42.     }  
  43.   
  44.     /** 
  45.      * @return the url 
  46.      */  
  47.     public String getUrl()  
  48.     {  
  49.         return this.url;  
  50.     }  
  51.   
  52.     /** 
  53.      * @param url 
  54.      *            the url to set 
  55.      */  
  56.     public void setUrl(String url)  
  57.     {  
  58.         this.url = url;  
  59.     }  
  60.   
  61.     /** 
  62.      * @return the version 
  63.      */  
  64.     public String getVersion()  
  65.     {  
  66.         return this.version;  
  67.     }  
  68.   
  69.     /** 
  70.      * @param version 
  71.      *            the version to set 
  72.      */  
  73.     public void setVersion(String version)  
  74.     {  
  75.         this.version = version;  
  76.     }  
  77.   
  78. }  

Doc.betwixt
xml 代码
 
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <info primitiveTypes="element">  
  3.     <element name="docResource">  
  4.         <attribute name="version" property="version" />  
  5.         <element name="contentUrl" property="url" />  
  6.         <addDefaults />  
  7.     </element>  
  8. </info>  

java 代码
 
  1. /** 
  2.  *  
  3.  */  
  4. package betwixt;  
  5.   
  6. import java.util.ArrayList;  
  7. import java.util.HashMap;  
  8. import java.util.List;  
  9. import java.util.Map;  
  10.   
  11. /** 
  12.  * @author huyunan 
  13.  *  
  14.  *  
  15.  * 2007 aspire 
  16.  */  
  17. public class Resource  
  18. {  
  19.   
  20.     private String version;  
  21.   
  22.     private String name;  
  23.   
  24.     private String author;  
  25.   
  26.     private Doc doc;  
  27.   
  28.     private Music music;  
  29.   
  30.     private List<Doc> docList;  
  31.   
  32.     private List<Music> musicList;  
  33.   
  34.     private HashMap<String, Doc> docMap;  
  35.   
  36.     private int docsSize;  
  37.   
  38.     private int musicsSize;  
  39.   
  40.     public Resource()  
  41.     {  
  42.         docList = new ArrayList<Doc>();  
  43.         musicList = new ArrayList<Music>();  
  44.         docMap = new HashMap<String, Doc>();  
  45.     }  
  46.   
  47.     public void addToDocMap(String key, Doc doc)  
  48.     {  
  49.         docMap.put(key, doc);  
  50.     }  
  51.   
  52.     public Map getDocMap()  
  53.     {  
  54.         return docMap;  
  55.     }  
  56.   
  57.     public void addToDocList(Doc doc)  
  58.     {  
  59.         docList.add(doc);  
  60.         docsSize = docList.size();  
  61.     }  
  62.   
  63.     public void addToMusicList(Music music)  
  64.     {  
  65.         musicList.add(music);  
  66.         musicsSize = musicList.size();  
  67.     }  
  68.   
  69.     /** 
  70.      * @return the docsSize 
  71.      */  
  72.     public int getDocsSize()  
  73.     {  
  74.         return this.docsSize;  
  75.     }  
  76.   
  77.     /** 
  78.      * @param docsSize 
  79.      *            the docsSize to set 
  80.      */  
  81.     public void setDocsSize(int docsSize)  
  82.     {  
  83.         this.docsSize = docsSize;  
  84.     }  
  85.   
  86.     /** 
  87.      * @return the docList 
  88.      */  
  89.     public List<Doc> getDocList()  
  90.     {  
  91.         return this.docList;  
  92.     }  
  93.   
  94.     /** 
  95.      * @return the name 
  96.      */  
  97.     public String getName()  
  98.     {  
  99.         return this.name;  
  100.     }  
  101.   
  102.     /** 
  103.      * @param name 
  104.      *            the name to set 
  105.      */  
  106.     public void setName(String name)  
  107.     {  
  108.         this.name = name;  
  109.     }  
  110.   
  111.     /** 
  112.      * @return the author 
  113.      */  
  114.     public String getAuthor()  
  115.     {  
  116.         return this.author;  
  117.     }  
  118.   
  119.     /** 
  120.      * @return the doc 
  121.      */  
  122.     public Doc getDoc()  
  123.     {  
  124.         return this.doc;  
  125.     }  
  126.   
  127.     /** 
  128.      * @param doc 
  129.      *            the doc to set 
  130.      */  
  131.     public void setDoc(Doc doc)  
  132.     {  
  133.         this.doc = doc;  
  134.     }  
  135.   
  136.     /** 
  137.      * @param author 
  138.      *            the author to set 
  139.      */  
  140.     public void setAuthor(String author)  
  141.     {  
  142.         this.author = author;  
  143.     }  
  144.   
  145.     /** 
  146.      * @return the version 
  147.      */  
  148.     public String getVersion()  
  149.     {  
  150.         return this.version;  
  151.     }  
  152.   
  153.     /** 
  154.      * @param version 
  155.      *            the version to set 
  156.      */  
  157.     public void setVersion(String version)  
  158.     {  
  159.         this.version = version;  
  160.     }  
  161.   
  162.     /** 
  163.      * @return the music 
  164.      */  
  165.     public Music getMusic()  
  166.     {  
  167.         return this.music;  
  168.     }  
  169.   
  170.     /** 
  171.      * @param music 
  172.      *            the music to set 
  173.      */  
  174.     public void setMusic(Music music)  
  175.     {  
  176.         this.music = music;  
  177.     }  
  178.   
  179.     /** 
  180.      * @return the musicList 
  181.      */  
  182.     public List<Music> getMusicList()  
  183.     {  
  184.         return this.musicList;  
  185.     }  
  186.   
  187.     /** 
  188.      * @return the musicsSize 
  189.      */  
  190.     public int getMusicsSize()  
  191.     {  
  192.         return this.musicsSize;  
  193.     }  
  194.   
  195.     /** 
  196.      * @param musicsSize 
  197.      *            the musicsSize to set 
  198.      */  
  199.     public void setMusicsSize(int musicsSize)  
  200.     {  
  201.         this.musicsSize = musicsSize;  
  202.     }  
  203.   
  204. }  

Resource.betwixt
xml 代码
 
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <info primitiveTypes="element">  
  3.     <element name="sync">  
  4.         <attribute name="version" property="version"/>  
  5.         <element name="resource">  
  6.             <!--   
  7.             <hide property="name" /> 
  8.             -->  
  9.             <element name="musicRes" property="music"/>  
  10.             <element name="docList">  
  11.                 <attribute name="docsSize" property="docsSize"/>  
  12.                 <element name="doc" property="docList"/>  
  13.             </element>  
  14.             <element name="musicList">  
  15.                 <attribute name="musicsSize" property="musicsSize"/>  
  16.                 <element name="music" property="musicList"/>  
  17.             </element>  
  18.             <element name="docMap">  
  19.                 <element name="doc" property="docMap"/>  
  20.             </element>  
  21.             <addDefaults/>  
  22.         </element>  
  23.     </element>  
  24. </info>  

测试代码
java 代码
 
  1. Doc doc = new Doc();  
  2.             doc.setName("doc_name");  
  3.             doc.setUrl("doc_url");  
  4.             doc.setVersion("1.0.0");  
  5.             Music music = new Music();  
  6.             music.setName("music_name");  
  7.             music.setMp3File("music_mp3file");  
  8.             Resource resource = new Resource();  
  9.             resource.setVersion("1.0.0");  
  10.             resource.setDoc(doc);  
  11.             resource.setMusic(music);  
  12.             resource.setName("resource_name");  
  13.             resource.setAuthor("resource_author");  
  14.             //  
  15.             resource.addToDocList(doc);  
  16.             resource.addToDocList(doc);  
  17.             resource.addToMusicList(music);  
  18.             resource.addToMusicList(music);  
  19.             //  
  20.             resource.addToDocMap("doc1", doc);  
  21.             // resource.addToMap("mucis1", music);  
  22.             //  
  23.             Writer outputWriter = new FileWriter("e:\\test.xml");  
  24.             BeanWriter beanWriter = new BeanWriter(outputWriter);  
  25.   
  26.             /** 
  27.              * beanWriter.getXMLIntrospector().getConfiguration().setElementNameMapper(new 
  28.              * org.apache.commons.betwixt.strategy.DecapitalizeNameMapper()); 
  29.              * beanWriter.getXMLIntrospector().getConfiguration().setElementNameMapper(new 
  30.              * org.apache.commons.betwixt.strategy.CapitalizeNameMapper()); 
  31.              * beanWriter.getXMLIntrospector().getConfiguration().setElementNameMapper(new 
  32.              * org.apache.commons.betwixt.strategy.HyphenatedNameMapper()); 
  33.              */  
  34.   
  35.             // beanWriter.getXMLIntrospector().getConfiguration().setPluralStemmer(new  
  36.             // ChinesePluralMapper());  
  37.             beanWriter.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false);  
  38.             beanWriter.getBindingConfiguration().setMapIDs(false);  
  39.             beanWriter.enablePrettyPrint();  
  40.             beanWriter.setEndTagForEmptyElement(true);  
  41.             beanWriter.setIndent("\t");  
  42.             //  
  43.             beanWriter.writeXmlDeclaration("<?xml version=\'1.0\' ?>");  
  44.             beanWriter.write(resource);  
  45.             outputWriter.close();  

输出结果
xml 代码
 
  1. <?xml version='1.0' ?>  
  2.     <sync version="1.0.0">  
  3.         <resource>  
  4.             <musicRes>  
  5.                 <mp3File>music_mp3file</mp3File>  
  6.                 <name>music_name</name>  
  7.             </musicRes>  
  8.             <docList docsSize="2">  
  9.                 <doc version="1.0.0">  
  10.                     <contentUrl>doc_url</contentUrl>  
  11.                     <docName>doc_name</docName>  
  12.                 </doc>  
  13.                 <doc version="1.0.0">  
  14.                     <contentUrl>doc_url</contentUrl>  
  15.                     <docName>doc_name</docName>  
  16.                 </doc>  
  17.             </docList>  
  18.             <musicList musicsSize="2">  
  19.                 <music>  
  20.                     <mp3File>music_mp3file</mp3File>  
  21.                     <name>music_name</name<sp>
分享到:
评论

相关推荐

    commons-betwixt-0.8的相关文件

    Apache Commons Betwixt 是一个Java库,它提供了一种方式来映射XML文档到Java对象,反之亦然。这个库使得开发人员可以方便地在XML数据和Java对象之间进行转换,大大简化了数据交换和序列化的过程。"commons-betwixt-...

    commons-betwixt-0.8.rar源文件及jar包

    Apache Commons Betwixt 是一个Java库,它提供了一种简单的方式来映射对象到XML以及从XML反向映射回对象。这个库是基于Apache软件基金会的Jakarta Commons项目,旨在简化XML数据绑定任务,使得开发者可以专注于业务...

    java使用commons-betwixt 实现bean与xml互转

    Java中的Apache Commons Betwixt库提供了一个简单的方法来映射Java Bean对象和XML文档之间的数据,使得在处理数据交换时能轻松地实现bean到XML的转换和XML到bean的反转换。本篇文章将深入探讨如何使用Commons ...

    commons-betwixt-0.5-src.zip_ObjectStringConvert_commons-betwixt_

    Apache Commons Betwixt 是一个Java库,它简化了JavaBean对象与XML文档之间的相互转换。这个工具包的核心功能在于,它允许开发者自动地将JavaBean的属性映射到XML结构,反之亦然,极大地提高了开发效率,尤其是在...

    commons-betwixt-0.8.jar

    commons-betwixt-0.8.jar

    betwixt详细使用说明

    此外,如果你需要将 XML 转换为 Java 对象,还需要引入 `commons-digester-1.8.jar`,因为 Betwixt 在进行反向转换时依赖于 Commons Digester。 接下来,我们来看一个简单的示例(example1),演示如何将一个 Java ...

    orgapache_commons

    包括commons-beanutils-1.8.0-bin、commons-betwixt-0.8、commons-cli-1.1、commons-codec-1.3、commons-collections-3.2.1-bin、commons-digester-1.8、commons-discovery-0.4、commons-email-1.1-bin、commons-...

    xml操作之betwixt实例

    **XML操作之Betwixt实例** XML(Extensible Markup Language)是一种用于标记数据的语言,广泛应用于数据交换、配置文件和文档存储等领域。在Java开发中,处理XML时,Betwixt是一个非常有用的库,它由Apache软件...

    org.apache.commons相关的所以jar包

    commons-betwixt-0.8.zip;commons-cli-1.1.zip;commons-codec-1.3.zip;commons-collections-3.2.1-bin.zip;commons-digester-1.8.zip;commons-discovery-0.4.zip;commons-email-1.1-bin.zip;commons-...

    xml转换java对象

    Apache Commons Betwixt库提供了一种简单的方法来实现这个转换。 Apache Commons Betwixt是Apache软件基金会的一个项目,它提供了一个用于XML到Java对象绑定的工具。通过使用Betwixt,开发者可以轻松地将XML文档...

    commons整理的常用的jar包,希望对你们有用

    commons-betwixt-0.8、 commons-cli-1.1、 commons-codec-1.3、 commons-collections-3.2.1-bin、 commons-digester-1.8、 commons-discovery-0.4、 commons-email-1.1-bin、 commons-fileupload-1.2.1-bin、...

    Apache Commons 所有包最新版本 含SRC (6/7)

    commons-attributes-2.2-src.zip commons-attributes-2.2.zip commons-beanutils-1.8.0-BETA-src.zip commons-beanutils-1.8.0-BETA.zip commons-betwixt-0.8-src.zip commons-betwixt-0.8.zip ...

    apache-commons源文件1,包括src,doc,jar,最新的

    commons-betwixt-0.8.zip commons-chain-1.2-bin.zip commons-cli-1.2-bin.zip commons-codec-1.7-bin.zip commons-collections-3.2.1-bin.zip commons-compress-1.4.1-bin.zip commons-configuration-1.9-bin.zip ...

    commons jar

    5. **commons-betwixt-0.8.jar**:Apache Commons Betwixt是一个对象到XML绑定工具,可以帮助将Java对象序列化为XML,以及从XML反序列化回Java对象。0.8版本提供了一种声明性的方式来描述对象和XML之间的映射。 6. ...

    Apache Commons 所有包最新版本 含SRC (5/7)

    commons-attributes-2.2-src.zip commons-attributes-2.2.zip commons-beanutils-1.8.0-BETA-src.zip commons-beanutils-1.8.0-BETA.zip commons-betwixt-0.8-src.zip commons-betwixt-0.8.zip ...

    apache-commons源码及jar文件

    Apache Commons是一个非常有用的工具包,解决各种实际的通用问题。(附件中提供了该工具包的jar包,及源文件以供研究) BeanUtils Commons-BeanUtils 提供对 Java 反射和自省API的包装 Betwixt Betwixt提供将 ...

    Apache Commons 所有包最新版本 含SRC (7/7)

    commons-attributes-2.2-src.zip commons-attributes-2.2.zip commons-beanutils-1.8.0-BETA-src.zip commons-beanutils-1.8.0-BETA.zip commons-betwixt-0.8-src.zip commons-betwixt-0.8.zip ...

    org.apache.commons.lang jar包下载(commons-lang3-3.1.jar)

    commons-lang3.3.1.jar、Apache Commons包中的一个,包含了一些数据类型工具类,是java.lang.*的扩展。必须使用的jar包。为JRE5.0+的更好的版本所提供 Jar文件包含的类: META-INF/MANIFEST.MFMETA-INF/LICENSE....

    Apache Commons 所有包最新版本 含SRC (1/7)

    commons-attributes-2.2-src.zip commons-attributes-2.2.zip commons-beanutils-1.8.0-BETA-src.zip commons-beanutils-1.8.0-BETA.zip commons-betwixt-0.8-src.zip commons-betwixt-0.8.zip ...

    apache commons jar(commons所有的jar包,从官网下载提供给大家)

    daemon-1.0.15-bin commons-dbutils-1.6-bin commons-digester3-3.2-bin commons-el-1.0 commons-email-1.4-bin commons-fileupload-1.0 commons-fileupload-1.1.1 commons-fileupload-1.1 commons-file upload-...

Global site tag (gtag.js) - Google Analytics