xml解析工具mashaller javaee自带解析类

1.怎样去掉Marshaller的格式化?

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

 

2.xml就像json解析一样,正常不过,是很正常的序列化格式。

 

posted @ 2017-10-03 16:04  跨境电商杂货铺  阅读(382)  评论(0编辑  收藏  举报