Transformer Java XML CDATA Duplicate Lines

时间:2012-11-27 20:32:43

标签: java xml transformer

我在JDK 7中使用变换器写出一些XML,我在CDATA中有一个freemarker模板,如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<fileExport append="false" automaticExport="false" automaticExportWithErrors="true" exportDir="C:\Users\bänn\Desktop" exportFileName="&lt;date&gt; &lt;time&gt; &lt;barcode&gt;" exportType="Excel" splitFiles="false">
  <exportTemplate><![CDATA[Date,RackBarcode,Row,Col,tubeBarcode,OrientationBarcode
<#list racks as rack>
<#list rack.containers as container>
${scandate?datetime},${rack.barcode},${container.textRow},${container.col + 1},${container.barcode},${rack.orientationBarcode}
</#list>
</#list>]]></exportTemplate>
  <scanTimeQuestionsTemplate><![CDATA[<#if scanTimeQuestionsEnabled>
Scan Time Questions

<#list scanTimeQuestions as question>
${question.shortName} : ${question.answer}
</#list>

</#if>]]></scanTimeQuestionsTemplate>
  <excelExportTemplate writeHeader="true">
    <excelExportColumn columnDataFormatting="d/M/yyyy HH:mm" columnDataFromString="DATETIME" columnHeader="Scan Time"/>
    <excelExportColumn columnDataFormatting="" columnDataFromString="RACK_BARCODE" columnHeader="Rack Barcode"/>
    <excelExportColumn columnDataFormatting="" columnDataFromString="ORIENTATION_BARCODE" columnHeader="Orientation Barcode"/>
    <excelExportColumn columnDataFormatting="" columnDataFromString="TUBE_ROW_TEXT" columnHeader="Tube Row"/>
    <excelExportColumn columnDataFormatting="" columnDataFromString="TUBE_COLUMN" columnHeader="Tube Column"/>
    <excelExportColumn columnDataFormatting="" columnDataFromString="TUBE_BARCODE" columnHeader="Tube Barcode"/>
    <excelExportColumn columnDataFormatting="d/M/yyy HH:mm:ss" columnDataFromString="DATETIME" columnHeader="Scan Datetime"/>
  </excelExportTemplate>
</fileExport>


</#if>

]]></scanTimeQuestionsTemplate>
  <excelExportTemplate writeHeader="false">
    <excelExportColumn columnDataFormatting="d/M/yyyy HH:mm" columnDataFromString="DATETIME" columnHeader="Scan Time"/>
    <excelExportColumn columnDataFormatting="" columnDataFromString="RACK_BARCODE" columnHeader="Rack Barcode"/>
    <excelExportColumn columnDataFormatting="" columnDataFromString="ORIENTATION_BARCODE" columnHeader="Orientation Barcode"/>
    <excelExportColumn columnDataFormatting="" columnDataFromString="TUBE_ROW_TEXT" columnHeader="Tube Row"/>
    <excelExportColumn columnDataFormatting="" columnDataFromString="TUBE_COLUMN" columnHeader="Tube Column"/>
    <excelExportColumn columnDataFormatting="" columnDataFromString="TUBE_BARCODE" columnHeader="Tube Barcode"/>
  </excelExportTemplate>
</fileExport>

然而,当我在Windows计算机上写出这个时,变压器会在输出的CDATA中添加一个额外的\ r \ n,所以我有:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<fileExport append="false" automaticExport="false" automaticExportWithErrors="false" exportDir="C:\Users\benn\Desktop" exportFileName="" exportType="text" splitFiles="false">
  <exportTemplate><![CDATA[Date,RackBarcode,Row,Col,tubeBarcode,OrientationBarcode

<#list racks as rack>

<#list rack.containers as container>

${scandate?datetime},${rack.barcode},${container.textRow},${container.col + 1},${container.barcode},${rack.orientationBarcode}

</#list>

</#list>

]]></exportTemplate>
  <scanTimeQuestionsTemplate><![CDATA[<#if scanTimeQuestionsEnabled>

Scan Time Questions



<#list scanTimeQuestions as question>

${question.shortName} : ${question.answer}

</#list>



</#if>

]]></scanTimeQuestionsTemplate>
  <excelExportTemplate writeHeader="false">
    <excelExportColumn columnDataFormatting="d/M/yyyy HH:mm" columnDataFromString="DATETIME" columnHeader="Scan Time"/>
    <excelExportColumn columnDataFormatting="" columnDataFromString="RACK_BARCODE" columnHeader="Rack Barcode"/>
    <excelExportColumn columnDataFormatting="" columnDataFromString="ORIENTATION_BARCODE" columnHeader="Orientation Barcode"/>
    <excelExportColumn columnDataFormatting="" columnDataFromString="TUBE_ROW_TEXT" columnHeader="Tube Row"/>
    <excelExportColumn columnDataFormatting="" columnDataFromString="TUBE_COLUMN" columnHeader="Tube Column"/>
    <excelExportColumn columnDataFormatting="" columnDataFromString="TUBE_BARCODE" columnHeader="Tube Barcode"/>
  </excelExportTemplate>
</fileExport>

所以基本上\ r \ n正在变成\ r \ n \ r \ n - 我已经看到一些关于Xalan这样做的消息,但我正在使用JDK 7提供的任何作为后引擎;有没有人看过这个,有没有我可以设置的属性来阻止变压器在我的CDATA中添加额外的换行符&gt;

1 个答案:

答案 0 :(得分:1)

我看过一些冗长的解决方法(搜索“xalan newline”),所以我会做什么:

  1. 确保源只有Unix行结尾\n(编辑器设置)。
  2. System.setProperty("file.encoding", "\n");
  3. 可能1.足够,因为2.很难看。