使用castor版本:1.3.1
原因:
查看castor-1.3.1-xml.jar中的castor.xml.properties(在\org\castor\xml目录下)
找到org.exolab.castor.xml.lenient.id.validation=false
发现默认是没有格式化的,生成的文件不直观、可读性差。
解决方法:
通过Marshaller修改validation的值为true
Marshaller marshaller = new Marshaller();
marshaller.setProperty("org.exolab.castor.indent", "true");
marshaller.setWriter(writer);
marshaller.setMapping(mapping);
marshaller.setEncoding("UTF-8");
marshaller.marshal(object);
注:marshaller.setProperty("org.exolab.castor.indent", "true");一定要放在marshaller.setWriter(writer);之前执行,否则不生效。
格式化后的文件就和eclipse的Ctrl+Shift+F一样了
本文介绍如何使用 Castor 1.3.1 版本的 Marshaller 类设置属性来美化生成的 XML 文件,使其更易阅读。通过将 org.exolab.castor.indent 属性设置为 true,可以实现 XML 文件的格式化。
210

被折叠的 条评论
为什么被折叠?



