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

前端之家收集整理的这篇文章主要介绍了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. @H_404_114@returnsw.toString();
  16. }catch(JAXBExceptione){
  17. log.error("",e);
  18. }
其中,类CharacterEscapeHandler为com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler

猜你在找的XML相关文章