如何在spring中从jaxb获取格式化的xml输出?

时间:2011-02-03 05:58:59

标签: spring jaxb

我使用Jaxb2Marshaller作为ContentNegotiatingViewResolver的视图属性。我能够得到xml repsonse。我如何格式化(漂亮打印)呢?

<bean
    class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="mediaTypes">
        <map>
            <entry key="xml" value="application/xml" />
        </map>
    </property>
    <property name="defaultViews">
        <list>

            <bean class="org.springframework.web.servlet.view.xml.MarshallingView">
                <constructor-arg>
                    <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
                        <property name="classesToBeBound">
                            <list>

                            </list>
                        </property>
                    </bean>
                </constructor-arg>
            </bean>
        </list>
    </property>

</bean>

5 个答案:

答案 0 :(得分:38)

<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>

答案 1 :(得分:22)

尝试在marshaller对象上设置此属性:

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

以下是Marshaller接口的完整Javadoc。查看字段摘要部分。

答案 2 :(得分:8)

Ritesh的回答对我不起作用。我必须做以下事情:

<bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="classesToBeBound">
        <list> ... </list>
    </property>
    <property name="marshallerProperties">
        <map>
            <entry key="jaxb.formatted.output">
                <value type="boolean">true</value>
            </entry>
        </map>
    </property>
</bean>

答案 3 :(得分:4)

正在寻找这个,并认为我会分享相同的代码

@Bean
public 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;
}

答案 4 :(得分:0)

达到目的的另一种方法是使用stringwriter类。

public class geoClientSpringWS extends WebServiceGatewaySupport
{

    public GetGeoIPResponse getGeoIPResponse(String inputval) throws JAXBException
    {
        String endpointuri="http://Kondle-PC:8088/mockGeoIPServiceSoap";
        GetGeoIP requestobj= new GetGeoIP();
        requestobj.setIPAddress(inputval);

        Jaxb2Marshaller marshaller= new Jaxb2Marshaller();

        StringWriter textwriter= new StringWriter();    
        JAXBContext jaxbContext = JAXBContext.newInstance(GetGeoIP.class);
        jaxbContext.createMarshaller().marshal(requestobj, textwriter);

        String textwriteroutput=textwriter.toString();

        System.out.println("response : " + textwriteroutput);   

        GetGeoIPResponse responseobj=(GetGeoIPResponse) getWebServiceTemplate().marshalSendAndReceive(endpointuri, requestobj);

        return responseobj;
    }
}