2013-10-03 27 views
0

我试图打印一个XML文件“原样”,例如,使用Apache FOP将标签转换为PDF。 我从ExampleXML2PDF例子文件开始,在FOP 1.1的src目录中。 我能够打印格式化的XML对象,不能打印带有标签的XML。在Apache FOP中将XML文件原样打印为PDF

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代码。

+1

是否使用XSLT样的榜样吗?如果是这样,请使用'CDATA'。如果您在您尝试输出文字XML的地方给出您的XSLT示例,则可以添加演示。 –

+0

感谢丹尼尔,我已编辑问题以获取代码示例。 –

回答

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) {} 
} 
+0

伙计,PDF不是纯文本。我正在使用Apache FOP以PDF格式导出/打印。 –

0

试试你的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> 
+0

不工作。我仍然失去所有的空白字符。 –

+0

@Tejasjain,尝试''。见http://www.w3.org/TR/xsl11/#white-space。 – mzjn