日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

本文介紹了來自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輸出不起作用,為什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,

分享到:
標簽:JAXB_FORMACTED_OUTPUT Jaxb2Marshaller Spring xml 不起作用 批處理 輸出
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定