Content is not allowed in prolog issue

This is my code for signing a 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);
    }
}

I am using it here

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);
}

But its showing error

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)

Its showing error in

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

I have read all the answers about this topic and they all are pointing towards the <?xml > tag having something before it but ... XML I am passing is starting with <?xml tag. No whitespaces So, why its showing error?

<?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>UEQ5NGJXd2dkbVZ5YzJsdmJqMGlNUzR3SWlCbGJtTnZaR2x1WnowaVZWUkdMVGdpSUhOMFlXNWtZV3h2Ym1VOUlubGxjeUkvUGp4QmRYUm9JSFZwWkQwaU9UazVPVGs1T1Rrd01ERTVJaUIwYVdROUluQjFZbXhwWXlJZ2RtVnlQU0l4TGpZaUlIUjRiajBpTUdZMVpXTmlNMk10WlRVMU15MDBaalF6TFdGaU5tVXRPVEEyTmpSa01qUXlORGRsSWlCNGJXeHVjejBpSUdoMGRIQTZMeTkzZDNjdWRXbGtZV2t1WjI5MkxtbHVMMkYxZEdobGJuUnBZMkYwYVc5dUwzVnBaQzFoZFhSb0xYSmxjWFZsYzNRdk1TNHdJQ0krUEUxbGRHRWdkV1JqUFNJaUlIQnBjRDBpSWlCbVpHTTlJazVCSWlCcFpHTTlJazVCSWlCc2IzUTlJbEFpSUd4dmRqMGlNVEV3TURBeUlpOCtQRk5yWlhrZ1kyazlJakl3TVRjd01qSTNJajVVUkhaMVkwRmtaelpOUTNSUlZqRlpkbEE1VEZkNGNrdEhlbEJKVTNoaFdHVlNZWEJyZWxwMk1sSklRVWQ2YVZkcVlYbDZRblpsYUVWYWRrRldWM2R1VVZwWFNERmFlalV3U3pGbVZEY3lUbFZ1WlhWSFlWQTNZVTFJZFRZMWIwNHdSVmhEZEdneWEyNUxVR2dyUVNzeFdGZzFObXRzUjJZeGFrTlFLMnhGUlhwR1VrSmpaRlJ2YVd4SVZYZDRUMmRYV0Zka2QwZHBablJMZFdaclVVUkpSbFJUTHk5aVZrTkpjRmRoYlZab1IyZHZWV2xDUzNOUloxSjJT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weU5HMDFiblJZTVRsc2NrZzJOMGxtTTBkMGRVOXFUSGN2TmtsNllqRTBjMU50TlVzNWVraDFhamxuYTB4dVYxUnFXR1JTUldodFZFVm9SakJhVFZOWGRrdFJWV1J2YmpaeGRtcElWelpZTjJnMVIzVkNVVWxrY1VsbmNGVlZVMjgyWVV0VVkzQlNVREEwUm10RldHZDJhbEpyV0c0NFFYSTFkWHBUWTNCbGRXZExWMjE1YkV4RVNEUkhPVE5CYVVaalJUUndabXhRZG10TFNDc3hSVTlZTkVoR01uazVVbnBtU1dWeFpuUXlhamxQWVhkMWFFUXhjekVyUjNoMFFWRXdabXBGYVhZNGQyVjJOMXBYVmxKcWMwbE5aMHBwYmxGVVNIZHJlRVJ2U1VoeGVITmlObE5qWkVRMWRYYzJkV2cyYUcxRFVrcDRVRTFIZW1aVVpFNVhaMGt5UTBOYWFXZG1VRFpFY1ZVMGRuRjZTSFZvYUZaM2JXNTVPSEppU3paWWRFSm9aV3haVWpoS2RqRnNNMlJYTjBaSGNUYzBiemxWYVhjOVBUd3ZSR0YwWVQ0OFNHMWhZejVrTW1NelpXMWFNazlHWnpCVVUzUlBXVlZPTW1Gc1NtdFVXSEJ5VlRCbmNsWnRjRFZqTURBMVdWVkdhRlo2WkRCU2JHUkNaVlZLUmxkSFdrNVRWVW93V1cweGRHRnNRalJOZWtreVkxVmtNbFZYV21oalFUMDlQQzlJYldGalBqd3ZRWFYwYUQ0PQ==</Aadhaar>
</Esign>

Real XML


ANSWERS:


One possibility would be that there is a BOM at the start of the file, try using some advanced editor to check current encoding and save the xml in UTF-8 without BOM.

Using Sublime Text, in the file menu, use save with encoding and choose "UTF-8":

screenshot

This should remove your BOM


I made some changes and now its working. But still didn't find why it was showing error in previous version.

previous Version-:

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

New Version -:

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

I Changed StringReader(xmlDocument) to File(XmlDocument)



 MORE:


 ? What is difference of between pulse-triggered FF(Flip Flop) and edge-triggered FF
 ? run a basic digital clock in the python shell
 ? Simplifying boolean algebra (XOR)
 ? Frank Vahid - Digital design - High level state machine write to storage example
 ? From VHDL to schematics?
 ? XOR Boolean algebra, simplifying
 ? Dspace import failed
 ? Use iText to create signature with complete own appearance
 ? Can I connect an IR sensor to my android phone?
 ? Digital Signature