本文介紹了來自Spring批處理Jaxb2Marshaller-Marshall.JAXB_FORMACTED_OUTPUT的XML輸出不起作用,為什么?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我正在進行將MongoDB轉換為XML的Spring批處理。我有以下兩個代碼片段。
首先:
<bean id="reportMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<value>com.mkyong.model.Report</value>
</property>
<property name="marshallerProperties">
<map>
<entry>
<key>
<util:constant static-field="javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT"/>
</key>
<value type="boolean">true</value>
</entry>
</map>
</property>
</bean>
第二個
<bean id="reportMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<value>com.mkyong.model.Report</value>
</property>
</bean>
兩種配置都沒有格式化輸出文件。結果只打印在一行XML文件中。我原以為第一個配置將格式化結果,但事實并非如此。
JAXB_FORMATTED_OUTPUT
用于指定編組的XML數據是否使用換行符和縮進進行格式化的屬性的名稱。但不幸的是,這種情況并未發生。
我在單行XML文件中看到的輸出:
pom.xml
<properties>
<!-- Generic properties -->
<java.version>1.7</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring.version>4.3.5.RELEASE</spring.version>
<spring.batch.version>3.0.7.RELEASE</spring.batch.version>
<spring.data.version>1.8.4.RELEASE</spring.data.version>
<mongodb.driver.version>3.1.1</mongodb.driver.version>
<!-- Logging -->
<logback.version>1.0.13</logback.version>
<slf4j.version>1.7.5</slf4j.version>
<jcl.slf4j.version>1.7.12</jcl.slf4j.version>
<!-- Test -->
<junit.version>4.12</junit.version>
</properties>
<dependencies>
<!-- Spring Core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring XML to/back object -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring Batch dependencies -->
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-core</artifactId>
<version>${spring.batch.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-infrastructure</artifactId>
<version>${spring.batch.version}</version>
</dependency>
<!-- Spring Batch unit test -->
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-test</artifactId>
<version>${spring.batch.version}</version>
</dependency>
<!-- MongoDB database driver -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>${mongodb.driver.version}</version>
</dependency>
<!-- Logging with SLF4J & LogBack -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${jcl.slf4j.version}</version>
</dependency>
<!-- Spring data mongodb -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>${spring.data.version}</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.12</version>
</dependency>
<!-- Junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- Testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.5</version>
<scope>test</scope>
</dependency>
</dependencies>
我正在使用以下內容:
<bean id="xmlItemWriter" class="org.springframework.batch.item.xml.StaxEventItemWriter">
<property name="resource" value="file:outputs/report.xml" />
<property name="encoding" value="ISO-8859-1" />
<property name="version" value="1.0" />
<property name="marshaller" ref="reportMarshaller" />
<property name="rootTagName" value="record" />
<!-- TRUE means, that output file will be overwritten if exists - default is TRUE -->
<property name="overwriteOutput" value="true" />
</bean>
根據Karthik的輸入:
<?xml version="1.0" encoding="ISO-8859-1"?>
<record><record id="1">
<date>01-06-2013</date>
<impression>139237</impression>
<clicks>40</clicks>
<earning>220.90</earning>
</record><record id="2">
<date>02-06-2013</date>
<impression>339100</impression>
<clicks>60</clicks>
<earning>320.88</earning>
</record><record id="3">
<date>03-06-2013</date>
<impression>431436</impression>
<clicks>76</clicks>
<earning>270.80</earning>
</record><record id="4">
<date>12-03-2016</date>
<impression>534987</impression>
<clicks>43</clicks>
<earning>454.80</earning>
</record></record>
推薦答案
看這個JIRA Ticket,這個問題在春批中還沒有修復。但是,有解決此問題的方法。
添加maven依賴項。
<dependency>
<groupId>net.java.dev.stax-utils</groupId>
<artifactId>stax-utils</artifactId>
<version>20070216</version>
</dependency>
創建自定義類。
public class IndentingStaxEventItemWriter <T> extends StaxEventItemWriter<T> {
private boolean indenting = true;
@Override
protected XMLEventWriter createXmlEventWriter( XMLOutputFactory outputFactory, Writer writer) throws XMLStreamException {
if (indenting) {
return new IndentingXMLEventWriter( super.createXmlEventWriter( outputFactory, writer ) );
}
else {
return super.createXmlEventWriter( outputFactory, writer );
}
}
public boolean isIndenting() {
return indenting;
}
public void setIndenting(boolean indenting) {
this.indenting = indenting;
}
}
修改您的EventWriter
<bean id="xmlItemWriter" class="com.kp.IndentingStaxEventItemWriter">
<property name="resource" value="file:outputs/report.xml" />
<property name="encoding" value="ISO-8859-1" />
<property name="version" value="1.0" />
<property name="marshaller" ref="reportMarshaller" />
<property name="rootTagName" value="record" />
<!-- TRUE means, that output file will be overwritten if exists - default is TRUE -->
<property name="overwriteOutput" value="true" />
</bean>
這篇關于來自Spring批處理Jaxb2Marshaller-Marshall.JAXB_FORMACTED_OUTPUT的XML輸出不起作用,為什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,