这是我签署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>UEQ5NGJXd2dkbVZ5YzJsdmJqMGlNUzR3SWlCbGJtTnZaR2x1WnowaVZWUkdMVGdpSUhOMFlXNWtZV3h2Ym1VOUlubGxjeUkvUGp4QmRYUm9JSFZwWkQwaU9UazVPVGs1T1Rrd01ERTVJaUIwYVdROUluQjFZbXhwWXlJZ2RtVnlQU0l4TGpZaUlIUjRiajBpTUdZMVpXTmlNMk10WlRVMU15MDBaalF6TFdGaU5tVXRPVEEyTmpSa01qUXlORGRsSWlCNGJXeHVjejBpSUdoMGRIQTZMeTkzZDNjdWRXbGtZV2t1WjI5MkxtbHVMMkYxZEdobGJuUnBZMkYwYVc5dUwzVnBaQzFoZFhSb0xYSmxjWFZsYzNRdk1TNHdJQ0krUEUxbGRHRWdkV1JqUFNJaUlIQnBjRDBpSWlCbVpHTTlJazVCSWlCcFpHTTlJazVCSWlCc2IzUTlJbEFpSUd4dmRqMGlNVEV3TURBeUlpOCtQRk5yWlhrZ1kyazlJakl3TVRjd01qSTNJajVVUkhaMVkwRmtaelpOUTNSUlZqRlpkbEE1VEZkNGNrdEhlbEJKVTNoaFdHVlNZWEJyZWxwMk1sSklRVWQ2YVZkcVlYbDZRblpsYUVWYWRrRldWM2R1VVZwWFNERmFlalV3U3pGbVZEY3lUbFZ1WlhWSFlWQTNZVTFJZFRZMWIwNHdSVmhEZEdneWEyNUxVR2dyUVNzeFdGZzFObXRzUjJZeGFrTlFLMnhGUlhwR1VrSmpaRlJ2YVd4SVZYZDRUMmRYV0Zka2QwZHBablJMZFdaclVVUkpSbFJUTHk5aVZrTkpjRmRoYlZab1IyZHZWV2xDUzNOUloxSjJTRFU1SzJWNVpuWnFWVnBNTURCdmVtRmtlbE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RVOXFUSGN2TmtsNllqRTBjMU50TlVzNWVraDFhamxuYTB4dVYxUnFXR1JTUldodFZFVm9SakJhVFZOWGRrdFJWV1J2YmpaeGRtcElWelpZTjJnMVIzVkNVVWxrY1VsbmNGVlZVMjgyWVV0VVkzQlNVREEwUm10RldHZDJhbEpyV0c0NFFYSTFkWHBUWTNCbGRXZExWMjE1YkV4RVNEUkhPVE5CYVVaalJUUndabXhRZG10TFNDc3hSVTlZTkVoR01uazVVbnBtU1dWeFpuUXlhamxQWVhkMWFFUXhjekVyUjNoMFFWRXdabXBGYVhZNGQyVjJOMXBYVmxKcWMwbE5aMHBwYmxGVVNIZHJlRVJ2U1VoeGVITmlObE5qWkVRMWRYYzJkV2cyYUcxRFVrcDRVRTFIZW1aVVpFNVhaMGt5UTBOYWFXZG1VRFpFY1ZVMGRuRjZTSFZvYUZaM2JXNTVPSEppU3paWWRFSm9aV3haVWpoS2RqRnNNMlJYTjBaSGNUYzBiemxWYVhjOVBUd3ZSR0YwWVQ0OFNHMWhZejVrTW1NelpXMWFNazlHWnpCVVUzUlBXVlZPTW1Gc1NtdFVXSEJ5VlRCbmNsWnRjRFZqTURBMVdWVkdhRlo2WkRCU2JHUkNaVlZLUmxkSFdrNVRWVW93V1cweGRHRnNRalJOZWtreVkxVmtNbFZYV21oalFUMDlQQzlJYldGalBqd3ZRWFYwYUQ0PQ==</Aadhaar>
</Esign>
答案 0 :(得分:0)
一种可能性是文件开头有一个BOM,尝试使用一些高级编辑器来检查当前编码并将xml保存为UTF-8而不用BOM。
使用Sublime Text,在文件菜单中,使用save with encoding并选择&#34; UTF-8&#34;:
这应该删除您的BOM
答案 1 :(得分:0)
我做了一些改动,现在正在努力。但仍然没有找到为什么它在以前的版本中显示错误。
之前的版本 - :
Document inputDocument = dbf.newDocumentBuilder().parse(new InputSource(new StringReader(xmlDocument)));
新版本 - :
Document inputDocument = dbf.newDocumentBuilder().parse(new InputSource(new File(XmlDocument));
我将StringReader(xmlDocument)更改为File(XmlDocument)