gpt4 book ai didi

java - Transformer Java XML CDATA 重复行

转载 作者:太空宇宙 更新时间:2023-11-04 07:56:12 30 4
gpt4 key购买 nike

我正在 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 中添加额外的换行符>

最佳答案

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

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

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

关于java - Transformer Java XML CDATA 重复行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13592539/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com