0

我正在尝试打印 XML 文件“AS-IS”,即 使用 Apache FOP 将标签转换为 PDF。我从FOP 1.1 的 src 目录中的ExampleXML2PDF示例文件开始。我能够打印格式化的 XML 对象,不能打印带有标签的 XML。

XSL如下,

<xsl:stylesheet version="2.0" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="testRaw" />
<xsl:template match="/">
    <fo:root>
        <fo:layout-master-set>
            <fo:simple-page-master master-name="my-page">
                <fo:region-body margin="0.5in" />
            </fo:simple-page-master>
        </fo:layout-master-set>
        <fo:page-sequence master-reference="my-page">
            <fo:flow flow-name="xsl-region-body" font="7pt Times">
                <fo:block break-before="page">
                    <xsl:value-of select="$testRaw" />
                </fo:block>
            </fo:flow>
        </fo:page-sequence>
    </fo:root>
</xsl:template>

用于设置参数的 Java 代码。

4

2 回答 2

1

尝试替换你xsl:value-ofxsl:copy-of并将其包装在CDATA.

例子...

<xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
<xsl:copy-of select="$testRaw" />
<xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
于 2013-10-07T15:54:24.497 回答
0

使用 bufferwriter,它应该看起来像这样:

public void readXML() {
            String name = "TheFile.xml";
            String path = "C:/User/Desktop/";
            File file = new File(path + name);
            if(file.canRead()) {
                String FileTxt = "";
                if((path + name).endsWith(".xml")) {
                    try {
                        Scanner read = new Scanner(new FileInputStream(file));
                        while (read.hasNextLine()) {
                            FileTxt += read.nextLine();
                        }
                        read.close();
                    } catch(FileNotFoundException e) {}
                    writePDF(FileTxt);
                }

            } else {
                System.out.print("Cannot Open File!");
            }
}

public void writePDF(String text) {
      String path = "C:/User/Desktop/"
      String name = "TheFile.pdf"
      if(!path.endsWith(name)) path += name;
                try {
                     BufferedWriter write = new BufferedWriter(new FileWriter(path));
                    write.write(text);
                    write.close();
                } catch(Exception e) {}
}
于 2013-10-03T10:37:42.157 回答