设置输出xml的格式 1 指定元素的格式
默认情况下 输出是按照write("xxx", yyy); 中xxx的方式输出 同时bean的首字母大写
例如
输出结果为
指定 NameMapper来重新定义大小写
输出结果为
DecapitalizeNameMapper 指定为小写
CapitalizeNameMapper 指定为大写
HyphenatedNameMapper 指定为连接符 比如 mp3File ==> mp3-file
使用 HyphenatedNameMapper 结果为
可以通过
设置首字母的大小写
实现NameMapper来定义自己的现实方式
默认情况下 输出是按照write("xxx", yyy); 中xxx的方式输出 同时bean的首字母大写
例如
java 代码
- 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 代码
- 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("/t");
- //
- beanWriter.writeXmlDeclaration("");
-
- beanWriter.write("Msg", music);
- outputWriter.close();
输出结果为
xml 代码
- xml version='1.0' ?>
- <Msg>
- <Music>
- <mp3File>music_mp3file<!--</span-->mp3File>
- <name>music_name<!--</span-->name>
- <!--</span-->Music>
- <!--</span-->Msg>
指定 NameMapper来重新定义大小写
java 代码
- beanWriter.getXMLIntrospector().getConfiguration().setElementNameMapper(new org.apache.commons.betwixt.strategy.DecapitalizeNameMapper());
输出结果为
xml 代码
- xml version='1.0' ?>
- <Msg>
- <music>
- <mp3File>music_mp3file<!--</span-->mp3File>
- <name>music_name<!--</span-->name>
- <!--</span-->music>
- <!--</span-->Msg>
DecapitalizeNameMapper 指定为小写
CapitalizeNameMapper 指定为大写
HyphenatedNameMapper 指定为连接符 比如 mp3File ==> mp3-file
使用 HyphenatedNameMapper 结果为
xml 代码
- xml version='1.0' ?>
- <Msg>
- <music>
- <mp3-file>music_mp3file<!--</span-->mp3-file>
- <name>music_name<!--</span-->name>
- <!--</span-->music>
- <!--</span-->Msg>
可以通过
java 代码
- HyphenatedNameMapper.setUpperCase(true)
实现NameMapper来定义自己的现实方式
java 代码
- class ItsANameMapper implements NameMapper {
- public String mapTypeToElementName(String typeName) {
- return " its_a_" + typeName;
- }
- }