如何在 spring 中从 jaxb 获取格式化的 xml 输出?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4882988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get formatted xml output from jaxb in spring?
提问by outvir
I am using Jaxb2Marshaller as a view property for ContentNegotiatingViewResolver. I am able to get the xml repsonse. How do I format (pretty print) it?
我使用 Jaxb2Marshaller 作为 ContentNegotiatingViewResolver 的视图属性。我能够获得 xml 响应。我如何格式化(漂亮的打印)它?
<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>
回答by Ritesh
<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>
回答by jmort253
Try setting this property on your marshaller object:
尝试在您的编组器对象上设置此属性:
marshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE )
Here is the full Javadoc for the Marshallerinterface. Check out the Field Summary section.
这是Marshaller接口的完整 Javadoc 。查看字段摘要部分。
回答by Doppelganger
Ritesh's answer didn't work for me. I had to do the following:
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>
回答by DeezCashews
Was looking for this and thought I'd share the code equivalent
正在寻找这个并认为我会分享等效的代码
@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;
}
回答by jnrprog
Use jaxb.formatted.output instead of javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT as
使用 jaxb.formatted.output 而不是 javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT 作为
Map<String,Object> map = new HashMap<String,Object>();
map.put("jaxb.formatted.output", true);
jaxb2Marshaller.setMarshallerProperties(map);
回答by sudhirkondle
Another way to achieve the purpose use stringwriter class.
另一种实现目的的方法是使用 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;
}
}

