Commons Betwixt 五步

本文介绍了一个使用Betwixt进行Java对象到XML映射的实例,展示了如何定义XML映射配置文件并实现复杂的数据结构转换。通过具体代码示例说明了不同类型的对象属性如何映射为XML元素。

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

稍微复杂一点的应用实例,idf项目所用的xml更为复杂头晕中
 
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;
    }


}

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 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>

/**
 * 
 
*/

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 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>

测试代码
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(
" ");
            
//
            beanWriter.writeXmlDeclaration("<?xml version='1.0' ?>");
            beanWriter.write(resource);
            outputWriter.close();

输出结果
<?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>
                
</music>
                
<music>
                    
<mp3File>music_mp3file</mp3File>
                    
<name>music_name</name>
                
</music>
            
</musicList>
            
<docMap>
                
<doc>
                    
<key>doc1</key>
                    
<value version="1.0.0">
                        
<contentUrl>doc_url</contentUrl>
                        
<docName>doc_name</docName>
                    
</value>
                
</doc>
            
</docMap>
            
<author>resource_author</author>
            
<doc version="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、付费专栏及课程。

余额充值