Jakarta-Common-BetWixt操纵笔记[Java编程]
本文“Jakarta-Common-BetWixt操纵笔记[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
以利用它把Javabean转换成XML并输出.
下载地址:http://commons.apache.org/beanutils/
代码以下:
Javabean:
package demo.bean;
public class Music ...{
private String name;
private String mp3File;
public String getName() ...{
return name;
}
public void setName(String name) ...{
this.name = name;
}
public String getMp3File() ...{
return mp3File;
}
public void setMp3File(String mp3File) ...{
this.mp3File = mp3File;
}
}
测试代码:
package demo;
import java.beans.IntrospectionException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import org.apache.commons.betwixt.io.BeanWriter;
import org.xml.sax.SAXException;
import demo.bean.Music;
public class BetwixtDemo ...{
public static void main(String[] args) throws IOException, SAXException, IntrospectionException ...{
Music music = new Music();
music.setName("music_name");
music.setMp3File("music_mp3file");
Writer outputWriter = new FileWriter("e:test.xml");
BeanWriter beanWriter = new BeanWriter(outputWriter);
beanWriter.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false);
beanWriter.getBindingConfiguration().setMapIDs(false);
beanWriter.enablePrettyPrint();
beanWriter.setEndTagForEmptyElement(true);
beanWriter.setIndent(" ");
beanWriter.writeXmlDeclaration("<?xml version="1.0" encoding="UTF-8" ?>");
beanWriter.write("Msg", music);
outputWriter.close();
}
}
可指定NameMapper来重新定义大小写
beanWriter.getXMLIntrospector().getConfiguration().setElementNameMapper(new org.apache.commons.betwixt.strategy.DecapitalizeNameMapper());
此中:
DecapitalizeNameMapper 指定义小写
CapitalizeNameMapper 指定为大写
HyphenatedNameMapper 指定为衔接符 比方mp3File ==> mp3-file
可以通过HyphenatedNameMapper.setUpperCase(true)设置首字母的大小写
也可实现NameMapper来定义自己的实际方法
如:class ItsANameMapper implements NameMapper { ...... }
以上是“Jakarta-Common-BetWixt操纵笔记[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |