Commons Betwixt 二步

设置输出xml的格式 1 指定元素的格式
默认情况下 输出是按照write("xxx", yyy); 中xxx的方式输出 同时bean的首字母大写
例如
java 代码
  1. public class Music
  2. {
  3. private String name;
  4. private String mp3File;
  5. public Music()
  6. {
  7. }
  8. /**
  9. * @return the name
  10. */
  11. public String getName()
  12. {
  13. return this.name;
  14. }
  15. /**
  16. * @param name
  17. * the name to set
  18. */
  19. public void setName(String name)
  20. {
  21. this.name = name;
  22. }
  23. /**
  24. * @return the mp3File
  25. */
  26. public String getMp3File()
  27. {
  28. return this.mp3File;
  29. }
  30. /**
  31. * @param mp3File
  32. * the mp3File to set
  33. */
  34. public void setMp3File(String mp3File)
  35. {
  36. this.mp3File = mp3File;
  37. }
  38. }

java 代码
  1. Music music = new Music();
  2. music.setName("music_name");
  3. music.setMp3File("music_mp3file");
  4. Writer outputWriter = new FileWriter("e://test.xml");
  5. BeanWriter beanWriter = new BeanWriter(outputWriter);


  6. beanWriter.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false);
  7. beanWriter.getBindingConfiguration().setMapIDs(false);
  8. beanWriter.enablePrettyPrint();
  9. beanWriter.setEndTagForEmptyElement(true);
  10. beanWriter.setIndent("/t");
  11. //
  12. beanWriter.writeXmlDeclaration("");

  13. beanWriter.write("Msg", music);
  14. outputWriter.close();

输出结果为
xml 代码
  1. xml version='1.0' ?>
  2. <Msg>
  3. <Music>
  4. <mp3File>music_mp3file<!--</span-->mp3File>
  5. <name>music_name<!--</span-->name>
  6. <!--</span-->Music>
  7. <!--</span-->Msg>

指定 NameMapper来重新定义大小写
java 代码
  1. beanWriter.getXMLIntrospector().getConfiguration().setElementNameMapper(new org.apache.commons.betwixt.strategy.DecapitalizeNameMapper());

输出结果为
xml 代码
  1. xml version='1.0' ?>
  2. <Msg>
  3. <music>
  4. <mp3File>music_mp3file<!--</span-->mp3File>
  5. <name>music_name<!--</span-->name>
  6. <!--</span-->music>
  7. <!--</span-->Msg>

DecapitalizeNameMapper 指定为小写
CapitalizeNameMapper 指定为大写
HyphenatedNameMapper 指定为连接符 比如 mp3File ==> mp3-file

使用
HyphenatedNameMapper 结果为
xml 代码
  1. xml version='1.0' ?>
  2. <Msg>
  3. <music>
  4. <mp3-file>music_mp3file<!--</span-->mp3-file>
  5. <name>music_name<!--</span-->name>
  6. <!--</span-->music>
  7. <!--</span-->Msg>

可以通过
java 代码
  1. HyphenatedNameMapper.setUpperCase(true)
设置首字母的大小写

实现NameMapper来定义自己的现实方式
java 代码
  1. class ItsANameMapper implements NameMapper {
  2. public String mapTypeToElementName(String typeName) {
  3. return " its_a_" + typeName;
  4. }
  5. }

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值