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

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念