java 序言中不允许引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25398554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:57:17  来源:igfitidea点击:

Reference is not allowed in prolog

javaxmlflying-saucer

提问by sunleo

Please help me to find the reason for this exception ,am using following jars,

请帮我找到这个异常的原因,我使用以下罐子,

core-renderer.jar
itext-paulo-155.jar

firstdoc.xhtml

第一个文档.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>My First Document</title>
        <style type="text/css"> b { color: green; } </style>
    </head>
    <body>
        <p>
            <b>Greetings Earthlings!</b>
            We've come for your Java.
        </p>
    </body>
</html>

JAVA :

爪哇:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.xhtmlrenderer.pdf.ITextRenderer;

import com.lowagie.text.DocumentException;

public class Sample1 {
    public static void main(String[] args)  throws IOException, DocumentException {
                String inputFile = "C:/Users/Joseph.M/WorkPlace_Struts2/Fly-Sauccer/src/test/pdf/firstdoc.xhtml";
                String url = new File(inputFile).toURI().toURL().toString();
                String outputFile = "C:/Users/Joseph.M/WorkPlace_Struts2/Fly-Sauccer/src/test/pdf/firstdoc.pdf";
                OutputStream os = new FileOutputStream(outputFile);             
                ITextRenderer renderer = new ITextRenderer();
                renderer.setDocument(url);
                renderer.layout();
                renderer.createPDF(os);             
                os.close();
    }
}

Exception :

例外 :

plumbing.render INFO:: Using CSS implementation from: org.xhtmlrenderer.context.StyleReference
plumbing.load INFO:: SAX XMLReader in use (parser): com.sun.org.apache.xerces.internal.parsers.SAXParser
plumbing.load INFO:: SAX XMLReader in use (parser): com.sun.org.apache.xerces.internal.parsers.SAXParser
plumbing.load INFO:: SAX XMLReader in use (parser): com.sun.org.apache.xerces.internal.parsers.SAXParser
plumbing.load INFO:: SAX XMLReader in use (parser): com.sun.org.apache.xerces.internal.parsers.SAXParser
plumbing.load INFO:: SAX XMLReader in use (parser): com.sun.org.apache.xerces.internal.parsers.SAXParser
plumbing.load INFO:: SAX XMLReader in use (parser): com.sun.org.apache.xerces.internal.parsers.SAXParser
plumbing.load INFO:: Reference is not allowed in prolog.
ERROR:  'Reference is not allowed in prolog.'
plumbing.exception WARNING:: Unhandled exception. Can't load the XML resource (using TRaX transformer). org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 2; Reference is not allowed in prolog.
Exception in thread "main" org.xhtmlrenderer.util.XRRuntimeException: Can't load the XML resource (using TRaX transformer). org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 2; Reference is not allowed in prolog.
    at org.xhtmlrenderer.resource.XMLResource$XMLResourceBuilder.createXMLResource(XMLResource.java:191)
    at org.xhtmlrenderer.resource.XMLResource.load(XMLResource.java:71)
    at org.xhtmlrenderer.swing.NaiveUserAgent.getXMLResource(NaiveUserAgent.java:118)
    at org.xhtmlrenderer.pdf.ITextRenderer.loadDocument(ITextRenderer.java:98)
    at org.xhtmlrenderer.pdf.ITextRenderer.setDocument(ITextRenderer.java:102)
    at test.pdf.Sample1.main(Sample1.java:23)
Caused by: javax.xml.transform.TransformerException: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 2; Reference is not allowed in prolog.
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source)
    at org.xhtmlrenderer.resource.XMLResource$XMLResourceBuilder.createXMLResource(XMLResource.java:189)
    ... 5 more
Caused by: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 2; Reference is not allowed in prolog.
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(Unknown Source)

回答by Danil Gaponov

Use < and > instead of

使用 < 和 > 代替

&lt; and &gt;

You do not have to escape XML special characters here.

您不必在此处转义 XML 特殊字符。