序言问题中不允许内容

Posted

技术标签:

【中文标题】序言问题中不允许内容【英文标题】:Content is not allowed in prolog issue 【发布时间】:2016-10-11 07:11:30 【问题描述】:

这是我签署 XML 的代码

public String signXML(String xmlDocument, boolean includeKeyInfo) 
    Security.addProvider(new BouncyCastleProvider());
    try 
        // Parse the input XML
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        Document inputDocument = dbf.newDocumentBuilder().parse(new InputSource(new StringReader(xmlDocument)));

        // Sign the input XML's DOM document
        Document signedDocument = sign(inputDocument, includeKeyInfo);

        // Convert the signedDocument to XML String
        StringWriter stringWriter = new StringWriter();
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer trans = tf.newTransformer();
        trans.transform(new DOMSource(signedDocument), new StreamResult(stringWriter));

        return stringWriter.getBuffer().toString();
     catch (Exception e) 
        e.printStackTrace();
        throw new RuntimeException("Error while digitally signing the XML document", e);
    

我在这里使用它

public static void main(String [] ags)

    //SignGenerator signgenerator=new SignGenerator();
    //signgenerator.generateXMLDigitalSignature("C:\\Users\\admin\\workspace\\Esign_Request\\Esign.xml", "C:\\Users\\admin\\workspace\\Esign_Request\\jaxb-emp.xml", "C:\\Users\\admin\\Downloads\\simple.p12", "C:\\Users\\admin\\Downloads\\uidai_auth_prod.cer");
    char[] a='p','u','b','l','i','c';
    DigitalSigner ds=new DigitalSigner("C:\\Users\\admin\\Downloads\\simple.p12",a,"public");
    String s=ds.signXML("C:\\Users\\admin\\workspace\\Esign_Request\\Esign.xml",true);
    System.out.println(s);

但显示错误

Fatal Error] :1:1: Content is not allowed in prolog.

org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)

显示错误

Document inputDocument = dbf.newDocumentBuilder().parse(new InputSource(new StringReader(xmlDocument)));

我已经阅读了关于这个主题的所有答案,他们都指向 <?xml > 标签之前有一些东西,但是......我传递的 XML 以 <?xml 标签开头。没有空格 那么,为什么会显示错误呢?

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Esign ts="2016-06-09T16:44:44" txn="78e65b0e-a20c-4fe2-b3cc-746a8e0c4670"  esignClass="1" aspId="KNTR-900" gatewayPin="" preferredCa="" sc="Y" ver="1.0">
    <Input>658c26712e1d4591253833407dc2cd81b8b686370d38b4c65c2ea6bdf54e5dd4</Input>
    <Aadhaar>UEQ5NGJXd2dkbVZ5YzJsdmJqMGlNUzR3SWlCbGJtTnZaR2x1WnowaVZWUkdMVGdpSUhOMFlXNWtZV3h2Ym1VOUlubGxjeUkvUGp4QmRYUm9JSFZwWkQwaU9UazVPVGs1T1Rrd01ERTVJaUIwYVdROUluQjFZbXhwWXlJZ2RtVnlQU0l4TGpZaUlIUjRiajBpTUdZMVpXTmlNMk10WlRVMU15MDBaalF6TFdGaU5tVXRPVEEyTmpSa01qUXlORGRsSWlCNGJXeHVjejBpSUdoMGRIQTZMeTkzZDNjdWRXbGtZV2t1WjI5MkxtbHVMMkYxZEdobGJuUnBZMkYwYVc5dUwzVnBaQzFoZFhSb0xYSmxjWFZsYzNRdk1TNHdJQ0krUEUxbGRHRWdkV1JqUFNJaUlIQnBjRDBpSWlCbVpHTTlJazVCSWlCcFpHTTlJazVCSWlCc2IzUTlJbEFpSUd4dmRqMGlNVEV3TURBeUlpOCtQRk5yWlhrZ1kyazlJakl3TVRjd01qSTNJajVVUkhaMVkwRmtaelpOUTNSUlZqRlpkbEE1VEZkNGNrdEhlbEJKVTNoaFdHVlNZWEJyZWxwMk1sSklRVWQ2YVZkcVlYbDZRblpsYUVWYWRrRldWM2R1VVZwWFNERmFlalV3U3pGbVZEY3lUbFZ1WlhWSFlWQTNZVTFJZFRZMWIwNHdSVmhEZEdneWEyNUxVR2dyUVNzeFdGZzFObXRzUjJZeGFrTlFLMnhGUlhwR1VrSmpaRlJ2YVd4SV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kZvMFUxQnpOWEZwVHk4dlpYSjBiWGxKVG5kME5uSnRVRmhrV1U0Mk4zZHZVVkZRTjJ3d09DczBUVU5rWlcweU5HMDFiblJZTVRsc2NrZzJOMGxtTTBkMGRVOXFUSGN2TmtsNllqRTBjMU50TlVzNWVraDFhamxuYTB4dVYxUnFXR1JTUldodFZFVm9SakJhVFZOWGRrdFJWV1J2YmpaeGRtcElWelpZTjJnMVIzVkNVVWxrY1VsbmNGVlZVMjgyWVV0VVkzQlNVREEwUm10RldHZDJhbEpyV0c0NFFYSTFkWHBUWTNCbGRXZExWMjE1YkV4RVNEUkhPVE5CYVVaalJUUndabXhRZG10TFNDc3hSVTlZTkVoR01uazVVbnBtU1dWeFpuUXlhamxQWVhkMWFFUXhjekVyUjNoMFFWRXdabXBGYVhZNGQyVjJOMXBYVmxKcWMwbE5aMHBwYmxGVVNIZHJlRVJ2U1VoeGVITmlObE5qWkVRMWRYYzJkV2cyYUcxRFVrcDRVRTFIZW1aVVpFNVhaMGt5UTBOYWFXZG1VRFpFY1ZVMGRuRjZTSFZvYUZaM2JXNTVPSEppU3paWWRFSm9aV3haVWpoS2RqRnNNMlJYTjBaSGNUYzBiemxWYVhjOVBUd3ZSR0YwWVQ0OFNHMWhZejVrTW1NelpXMWFNazlHWnpCVVUzUlBXVlZPTW1Gc1NtdFVXSEJ5VlRCbmNsWnRjRFZqTURBMVdWVkdhRlo2WkRCU2JHUkNaVlZLUmxkSFdrNVRWVW93V1cweGRHRnNRalJOZWtreVkxVmtNbFZYV21oalFUMDlQQzlJYldGalBqd3ZRWFYwYUQ0PQ==</Aadhaar>
</Esign>

【问题讨论】:

我正在使用 sublime。它没有显示任何内容 请重新检查您的文件并告诉我们第一个&lt; 对应的字节(请不要猜测,如有疑问请尝试使用十六进制编辑器)。它不是你前面有东西的第一个字节,如果你在文本编辑器中看不到它可能是一个 BOM,就像 RC 指出的那样。 如何删除 BOM 并识别它。 我通过删除前三位数字创建了一个新的 XML 文件,如果有任何 BOM,它就删除了“ 尝试使用 Notepad++ 整理您的文件。如果有BOM,应该显示为UTF-8 with BOM。 【参考方案1】:

一种可能性是文件开头有一个BOM,尝试使用一些高级编辑器检查当前编码并将xml保存为UTF-8而不使用BOM。

使用 Sublime Text,在文件菜单中,使用 save with encoding 并选择“UTF-8”:

这应该会删除您的 BOM

【讨论】:

【参考方案2】:

我做了一些更改,现在它可以工作了。但是还是没找到以前版本显示错误的原因。

以前的版本-:

   Document inputDocument = dbf.newDocumentBuilder().parse(new InputSource(new StringReader(xmlDocument)));

新版本-:

   Document inputDocument = dbf.newDocumentBuilder().parse(new InputSource(new File(XmlDocument));

我将 StringReader(xmlDocument) 更改为 File(XmlDocument)

【讨论】:

以上是关于序言问题中不允许内容的主要内容,如果未能解决你的问题,请参考以下文章

致命错误:1:1:序言中不允许内容

Java“序言中不允许的内容。”

StaX:序言中不允许的内容

错误的常春藤文件 - 序言中不允许内容

序言中不允许内容,但序言在运行时可以吗? Groovy,Jenkins,Java,管道,XML [重复]

序言中不能有内容。在安卓工作室