- Thread Author
- #1
I have searched on internet to find solution of my problem but I did not find any solution.I want to transform xml using xsl in android or ios with dart.Following code is for transform xml using xsl in java
I have tried xsltProcessor but it has deprecated.
Code:
public String getInvoiceHash(String xmlDocument, Context mContext) throws TransformerException, IOException {
Transformer transformer = this.getTransformer(mContext);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
StreamResult xmlOutput = new StreamResult(byteArrayOutputStream);
transformer.transform(new StreamSource(new StringReader(xmlDocument)), xmlOutput);
MessageDigest digest = null;
byte[] hashSHA256 = null;
try {
digest = MessageDigest.getInstance("SHA-256");
hashSHA256 = digest.digest(byteArrayOutputStream.toByteArray());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
return Base64.getEncoder().encodeToString(hashSHA256);
}else{
return new String(
android.util.Base64.encode(hashSHA256, android.util.Base64.DEFAULT),
StandardCharsets.UTF_8
);
}
}
private Transformer getTransformer(Context mContext) throws TransformerConfigurationException {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
InputStream XmlFileInputStream = mContext.getResources().openRawResource(R.raw.invoice);
Transformer transformer = transformerFactory.newTransformer(new StreamSource(XmlFileInputStream));
transformer.setOutputProperty("encoding", "UTF-8");
transformer.setOutputProperty("indent", "no");
transformer.setOutputProperty("omit-xml-declaration", "yes");
return transformer;
}
I have tried xsltProcessor but it has deprecated.