How to get formatted xml output from jaxb in spring? How to get formatted xml output from jaxb in spring? spring spring

How to get formatted xml output from jaxb in spring?


<bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">    <property name="classesToBeBound">        <list> .... </list>    </property>    <property name="marshallerProperties">        <map>            <entry>                <key>                    <util:constant static-field="javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT" />               </key>              <value type="java.lang.Boolean">true</value>            </entry>        </map>    </property></bean>


Try setting this property on your marshaller object:

 marshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE )

Here is the full Javadoc for the Marshaller interface. Check out the Field Summary section.


Was looking for this and thought I'd share the code equivalent

@Beanpublic Marshaller jaxbMarshaller() {    Map<String, Object> props = new HashMap<String, Object>();    props.put(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);    Jaxb2Marshaller m = new Jaxb2Marshaller();    m.setMarshallerProperties(props);    m.setPackagesToScan("com.example.xml");    return m;}