How to send or receive xml file in Flutter?

4,425

Thanks to @GünterZöchbauer. I mange to build xml in Flutter and be able to post and get the response. Here is the code:

DEPENDENCIES:

// Add pubspec.yaml:   xml: "^3.2.1"
import 'package:xml/xml.dart' as xml;
import 'dart:io';

BUILD XML:

// TODO: BUILD XML FILE
Future<HttpClientResponse> _sendOTP() async {
  var builder = new xml.XmlBuilder();
  builder.processing('xml', 'version="1.0" encoding="iso-8859-9"');
  builder.element('MainmsgBody', nest: () {
    builder.element('UserName', nest: “xxxxxxxx”);
    builder.element('PassWord', nest: “yyyyyyyy”);
    builder.element('Action', nest: 5);
    builder.element('Mesgbody', nest: “I am Fluttering with Dart”);
    builder.element('Numbers', nest: 5);
  });
  var bookshelfXml = builder.build();
  String _uriMsj = bookshelfXml.toString();
  print("_uriMsj: $_uriMsj");

  String _uri = "https://*******.******.com/http****”;
  var _responseOtp = postOTP(_uri, _uriMsj);
  print("_responseOtp: $_responseOtp");
}


**POST XML:**
 // TODO: POST XML FILE
Future<String> postOTP(String _uri, String _message) async {
  HttpClient client = new HttpClient();
  HttpClientRequest request = await client.postUrl(Uri.parse(_uri));
  request.write(_message);
  HttpClientResponse response = await request.close();
  StringBuffer _buffer = new StringBuffer();
  await for(String a in await response.transform(utf8.decoder)) {
    _buffer.write(a);
  }
  print("_buffer.toString: ${_buffer.toString()}");
  return _buffer.toString();
}
Share:
4,425
Nick
Author by

Nick

Updated on December 07, 2022

Comments

  • Nick
    Nick over 1 year

    I can send and receive JSON, string data with Flutter. But I cannot find any information how to send and receive xml file with Flutter.

    I am looking good documentation and basic hands-on-example. Any help please?