微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Marshaller生成的xml去掉报文头、设置格式、不处理转义字符的方法

http://blog.csdn.net/qustmeng/article/details/53706657

  1. try{
  2. JAXBContextcontext=JAXBContext.newInstance(Entity.class);
  3. Marshallermarshaller=context.createMarshaller();
  4. //xml格式
  5. marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
  6. //去掉生成xml的认报文头
  7. marshaller.setProperty(Marshaller.JAXB_FRAGMENT,0); background-color:inherit">//不进行转义字符的处理
  8. marshaller.setProperty(CharacterEscapeHandler.class.getName(),newCharacterEscapeHandler(){
  9. publicvoidescape(char[]ch,153); font-weight:bold; background-color:inherit">intstart,153); font-weight:bold; background-color:inherit">intlength,153); font-weight:bold; background-color:inherit">booleanisAttVal,Writerwriter)throwsIOException{
  10. writer.write(ch,start,length);
  11. }
  12. });
  13. StringWritersw=newStringWriter();
  14. marshaller.marshal(entity,sw);
  15. returnsw.toString();
  16. }catch(JAXBExceptione){
  17. log.error("",e);
  18. }
其中,类CharacterEscapeHandler为com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler

原文地址:https://www.jb51.cc/xml/293606.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。