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

来源:互联网 发布:芮成钢间谍知乎 编辑:程序博客网 时间:2024/05/21 09:19

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

  1. try {  
  2.             JAXBContext context = JAXBContext.newInstance(Entity.class);  
  3.             Marshaller marshaller = context.createMarshaller();  
  4.             // xml格式  
  5.             marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);  
  6.             // 去掉生成xml的默认报文头  
  7.             marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);  
  8.             // 不进行转义字符的处理  
  9.             marshaller.setProperty(CharacterEscapeHandler.class.getName(), new CharacterEscapeHandler() {  
  10.                 public void escape(char[] ch, int start,int length, boolean isAttVal, Writer writer) throws IOException {  
  11.                     writer.write(ch, start, length);  
  12.                 }  
  13.             });  
  14.             StringWriter sw = new StringWriter();  
  15.             marshaller.marshal(entity, sw);  
  16.             return sw.toString();  
  17.         } catch (JAXBException e) {  
  18.             log.error("", e);  
  19.         } 
其中,类CharacterEscapeHandler为com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler

原创粉丝点击