1.怎样去掉Marshaller的格式化?
:
- JAXBContext context = JAXBContext.newInstance(Entity.class);
- Marshaller marshaller = context.createMarshaller();
- // xml格式
- marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
- // 去掉生成xml的默认报文头
- marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
- // 不进行转义字符的处理
- marshaller.setProperty(CharacterEscapeHandler.class.getName(), new CharacterEscapeHandler() {
- public void escape(char[] ch, int start,int length, boolean isAttVal, Writer writer) throws IOException {
- writer.write(ch, start, length);
- }
- });
- StringWriter sw = new StringWriter();
- marshaller.marshal(entity, sw);
- return sw.toString();
- } catch (JAXBException e) {
- log.error("", e);
- }
2.xml就像json解析一样,正常不过,是很正常的序列化格式。