Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

How to transform xml using xsl in android and ios with dart flutter

New member
Joined
Feb 27, 2023
Messages
2
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

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.
 
New member
Joined
Feb 21, 2023
Messages
2
EDIT:

xml_transformer package is only available for Dart, which can be used in non-Flutter projects.

To transform XML using XSL in Flutter, you can use the dart:xml library instead of the xml package. Here's an example of how to use it:

  1. Import the dart:xml library:
Code:
import 'dart:xml';
Code:
Create an XML file and an XSL file. For example, let's say you have an XML file called data.xml:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<root>
  <item>
    <name>Item 1</name>
    <price>40.00</price>
  </item>
  <item>
    <name>Item 2</name>
    <price>80.00</price>
  </item>
</root>
And an XSL file called transform.xsl:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <html>
      <body>
        <table>
          <tr>
            <th>Name</th>
            <th>Price</th>
          </tr>
          <xsl:for-each select="root/item">
            <tr>
              <td><xsl:value-of select="name"/></td>
              <td><xsl:value-of select="price"/></td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>
  1. Load the XML and XSL files into your Dart code:
Code:
final xmlFile = await rootBundle.loadString('assets/data.xml');
final xslFile = await rootBundle.loadString('assets/transform.xsl');

final xml = XmlDocument.parse(xmlFile);
final xsl = XsltProcessor.parse(xslFile);
Note that we're using the rootBundle to load the files. Make sure to include the XML and XSL files in your Flutter project's pubspec.yaml file and specify their asset locations:

Code:
flutter:
  assets:
    - assets/data.xml
    - assets/transform.xsl
4)Transform the XML using the XSL and get the result as a string:


Code:
final result = xsl.transform(xml);
final output = result.toString();
5)Display the result in your app. You can use a WebView to display HTML content:




Code:
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

class MyWidget extends StatelessWidget {
  final String html;

  const MyWidget({required this.html});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: WebView(
        initialUrl: 'about:blank',
        onWebViewCreated: (controller) {
          controller.loadUrl(
            Uri.dataFromString(html, mimeType: 'text/html', encoding: Encoding.getByName('utf-8')).toString(),
          );
        },
      ),
    );
  }
}
  1. Finally, display the MyWidget in your app:

Code:
final widget = MyWidget(html: output);
happy coding...
 
Top