您的位置:首页 > 其它

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

2017-11-27 15:56 363 查看
http://blog.csdn.net/qustmeng/article/details/53706657

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐