从jaxb xml响应中删除空格

时间:2013-05-21 10:05:44

标签: java xml web-services axis2

我有WS生成SOAP XML条消息。由于大小限制,我想删除不必要的空格(用于缩进)和新行。如何在使用生成的类和注释(@WebService@WebMethod)时执行此操作?在示例中,我看到它是这样完成的:

Marshaller m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE);

但是,我不是手动创建Marshaller所以我不知道在哪里可以添加此属性,如果这是正确的方法。 JAXB实施是axis2

2 个答案:

答案 0 :(得分:1)

创建一个自定义JAXBContext并注释您的webservice,如下所述:

    @WebService(serviceName = "Hello")
    @UsesJAXBContext(value = CustomJaxbContext.class)
    public class HelloWS
    {   ...
    }

    public class HelloJaxbContext implements JAXBContextFactory
    {
      @Override
      public JAXBRIContext createJAXBContext(SEIModel seim, List<Class> classesToBind, List<TypeReference> typeReferences) throws JAXBException {
          //JAXBRIContext extends JAXBContext, so you should be able to set the desired marshaller properties
          //create your jaxb context with necessary properties for marshalling
          return yourJAXBRIContext;
       }  
    }

参考http://javasourcecode.org/html/open-source/jdk/jdk-6u23/com/sun/xml/internal/ws/developer/JAXBContextFactory.html

答案 1 :(得分:0)

<dataFormats><jaxb prettyPrint=false></dataFormats>这个漂亮的Print标志将以压缩方式格式化xml文件并删除crlf