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

猜你喜欢

转载自blog.csdn.net/qq501569325/article/details/106231159
今日推荐