1

我在 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,所以我有:

<?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\r\n - 我已经看到一些关于 Xalan 这样做的消息,但我正在使用 JDK 7 提供的任何东西作为后台引擎;有没有人看到这个,我可以设置一个属性来停止变压器在我的 CDATA 中放置一个额外的换行符>

4

1 回答 1

1

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

  1. 确保源代码只有 Unix 行尾\n(编辑器设置)。
  2. System.setProperty("file.encoding", "\n");

可能 1. 就足够了,因为 2. 很丑。

于 2012-11-27T21:08:37.113 回答