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

try {
			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);
		}


其中,类CharacterEscapeHandler为com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler

你可能感兴趣的:(Java)