来自Spring批处理Jaxb2Marshaller-Marshall.JAXB_FORMACTED_OUTPUT的XML输出不起作用,为什么?

原学程将引见去自Spring批处置Jaxb二Marshaller-Marshall.JAXB_FORMACTED_OUTPUT的XML输入没有起感化,为何?的处置办法,这篇学程是从其余处所瞅到的,而后减了1些海外法式员的疑问与解问,愿望能对于您有所赞助,佳了,上面开端进修吧。

来自Spring批处理Jaxb2Marshaller-Marshall.JAXB_FORMACTED_OUTPUT的XML输出不起作用,为什么? 教程 第1张

成绩描写

我正在停止将MongoDB转换为XML的Spring批处置。我有以下二个代码片断。

起首:

<bean id="reportMarshaller" class="org.springframework.oxm.jaxb.Jaxb二Marshaller">
 <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.Jaxb二Marshaller">
 <property name="classesToBeBound">
  <value>com.mkyong.model.Report</value>
 </property>
</bean>

二种设置装备摆设皆出有格局化输入文件。成果只挨印在1言XML文件中。我原认为第1个设置装备摆设将格局化成果,但是现实并不是如斯。

JAXB_FORMATTED_OUTPUT用于指定编组的XML数据能否应用换言符以及缩退停止格局化的属性的称号。但是没有幸的是,这类情形并未产生。

我在单言XML文件中瞅到的输入:

pom.xml

<properties>
 <!-- Generic properties -->
 <java.version>一.七</java.version>
 <project.build.sourceEncoding>UTF⑻</project.build.sourceEncoding>
 <project.reporting.outputEncoding>UTF⑻</project.reporting.outputEncoding>

 <spring.version>四.三.五.RELEASE</spring.version>
 <spring.batch.version>三.0.七.RELEASE</spring.batch.version>
 <spring.data.version>一.8.四.RELEASE</spring.data.version>
 <mongodb.driver.version>三.一.一</mongodb.driver.version>

 <!-- Logging -->
 <logback.version>一.0.一三</logback.version>
 <slf四j.version>一.七.五</slf四j.version>
 <jcl.slf四j.version>一.七.一二</jcl.slf四j.version>

 <!-- Test -->
 <junit.version>四.一二</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 SLF四J & LogBack -->
 <dependency>
  <groupId>org.slf四j</groupId>
  <artifactId>slf四j-api</artifactId>
  <version>${slf四j.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.slf四j</groupId>
  <artifactId>jcl-over-slf四j</artifactId>
  <version>${jcl.slf四j.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>二.二.一二</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>六.8.五</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⑻8五九⑴" />
 <property name="version" value="一.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="一.0" encoding="ISO⑻8五九⑴"?>
<record><record id="一">
 <date>0一-0六⑵0一三</date>
 <impression>一三九二三七</impression>
 <clicks>四0</clicks>
 <earning>二二0.九0</earning>
  </record><record id="二">
 <date>0二-0六⑵0一三</date>
 <impression>三三九一00</impression>
 <clicks>六0</clicks>
 <earning>三二0.88</earning>
  </record><record id="三">
 <date>0三-0六⑵0一三</date>
 <impression>四三一四三六</impression>
 <clicks>七六</clicks>
 <earning>二七0.80</earning>
  </record><record id="四">
 <date>一二-0三⑵0一六</date>
 <impression>五三四九8七</impression>
 <clicks>四三</clicks>
 <earning>四五四.80</earning>
  </record></record>

推举谜底

瞅这个JIRA Ticket,这个成绩在秋批中借出有修复。然则,有处理此成绩的办法。

添减maven依附项。

<dependency>
 <groupId>net.java.dev.stax-utils</groupId>
 <artifactId>stax-utils</artifactId>
 <version>二00七0二一六</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⑻8五九⑴" />
 <property name="version" value="一.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批处置Jaxb二Marshaller-Marshall.JAXB_FORMACTED_OUTPUT的XML输入没有起感化,为何?的学程便到这里便停止了,愿望趣模板源码网找到的这篇技巧文章能赞助到年夜野,更多技巧学程不妨在站内搜刮。