如何在Flutter中发送或接收xml文件?

时间:2018-09-24 06:33:18

标签: xml https flutter

我可以使用Flutter发送和接收JSON字符串数据。但是我找不到任何有关如何使用Flutter发送和接收xml文件的信息。

我正在寻找好的文档和基本的实际操作示例。有什么帮助吗?

1 个答案:

答案 0 :(得分:3)

感谢@GünterZöchbauer。我想在Flutter中建立xml,并且能够发布并获得响应。这是代码:

依赖项:

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

内置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();
}