加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 01:53:19 所属栏目:百科 来源:网络整理
导读:http://blog.csdn.net/qustmeng/article/details/53706657 try { JAXBContextcontext=JAXBContext.newInstance(Entity. class ); Marshallermarshaller=context.createMarshaller(); //xml格式 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, tr

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读