如何将xml文件导入到dart对象

Salman aboholiqah

我正在尝试使用xml包将c#XmlSerializer导出的xml文件导入dart对象,但是所有导入尝试均失败。

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfViewReads xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ViewReads>
    <CounterAccount>7e7386db-ae4e-47f8-a473-8a5534b8b421</CounterAccount>
    <ParentCounter>00000000-0000-0000-0000-000000000000</ParentCounter>
    <CounterGuid>88165f87-113b-4503-bd95-01cfc778cf0e</CounterGuid>
    <CounterID>354</CounterID>
    <AccountName>يحي العامر</AccountName>
    <AreaName>مربع الشارع العام  وغزه</AreaName>
    <AddressDesc>منطقه الوء الاخضر</AddressDesc>
    <Phone>771707009</Phone>
    <FazName>سنجل فاز</FazName>
    <Balance>865.0000</Balance>
    <LastRead>7332.00</LastRead>
  </ViewReads>
  <ViewReads>
    <CounterAccount>46e2bacc-644f-47fb-abe7-6589f880667e</CounterAccount>
    <ParentCounter>00000000-0000-0000-0000-000000000000</ParentCounter>
    <CounterGuid>2d2f1a40-9dcf-4a3c-9fd2-081d3be83aaa</CounterGuid>
    <CounterID>2052</CounterID>
    <AccountName>كمال محمد علي ثامر</AccountName>
    <AreaName>مربع الشارع العام  وغزه</AreaName>
    <AddressDesc>خلف كوافير السلطانه</AddressDesc>
    <Phone>771363922</Phone>
    <FazName>سنجل فاز</FazName>
    <Balance>1560.0000</Balance>
    <LastRead>84.00</LastRead>
    <UserGuid>00000000-0000-0000-0000-000000000000</UserGuid>
  </ViewReads>
</ArrayOfViewReads>

这是我使用VSCode生成的fromJson()方法从dart导入代码的方法:

  final file = File('C:\\Users\\SALMAN\\Desktop\\2021_1_19_559.xml');
  final document = XmlDocument.parse(file.readAsStringSync());

  document.children.forEach((e) {
    var v = ViewRead.fromJson(e.toString());
  });

这是我尝试运行代码后的错误:

Unhandled exception:
FormatException: Unexpected character (at character 1)
<?xml version="1.0" encoding="utf-8"?>
^

#0      _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1404:5)
#1      _ChunkedJsonParser.parseNumber (dart:convert-patch/convert_patch.dart:1271:9)
#2      _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:936:22)
#3      _parseJson (dart:convert-patch/convert_patch.dart:40:10)
#4      JsonDecoder.convert (dart:convert/json.dart:505:36)
#5      JsonCodec.decode (dart:convert/json.dart:156:41)
#6      new ViewRead.fromJson (file:///C:/Users/SALMAN/.IntelliJIdea2019.1/dart/bin/dart.dart:170:70)
#7      main.<anonymous closure> (file:///C:/Users/SALMAN/.IntelliJIdea2019.1/dart/bin/dart.dart:16:22)
#8      List.forEach (dart:core-patch/growable_array.dart:313:8)
#9      _DelegatingIterableBase.forEach (package:collection/src/wrappers.dart:52:45)
#10     main (file:///C:/Users/SALMAN/.IntelliJIdea2019.1/dart/bin/dart.dart:15:21)
#11     _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:299:32)
#12     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)

julemand101

这是一个示例,说明如何使用xml来自以下位置来解析此XML https//pub.dev/packages/xml

import 'package:xml/xml.dart';

const xml = '''
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfViewReads xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ViewReads>
    <CounterAccount>7e7386db-ae4e-47f8-a473-8a5534b8b421</CounterAccount>
    <ParentCounter>00000000-0000-0000-0000-000000000000</ParentCounter>
    <CounterGuid>88165f87-113b-4503-bd95-01cfc778cf0e</CounterGuid>
    <CounterID>354</CounterID>
    <AccountName>يحي العامر</AccountName>
    <AreaName>مربع الشارع العام  وغزه</AreaName>
    <AddressDesc>منطقه الوء الاخضر</AddressDesc>
    <Phone>771707009</Phone>
    <FazName>سنجل فاز</FazName>
    <Balance>865.0000</Balance>
    <LastRead>7332.00</LastRead>
  </ViewReads>
  <ViewReads>
    <CounterAccount>46e2bacc-644f-47fb-abe7-6589f880667e</CounterAccount>
    <ParentCounter>00000000-0000-0000-0000-000000000000</ParentCounter>
    <CounterGuid>2d2f1a40-9dcf-4a3c-9fd2-081d3be83aaa</CounterGuid>
    <CounterID>2052</CounterID>
    <AccountName>كمال محمد علي ثامر</AccountName>
    <AreaName>مربع الشارع العام  وغزه</AreaName>
    <AddressDesc>خلف كوافير السلطانه</AddressDesc>
    <Phone>771363922</Phone>
    <FazName>سنجل فاز</FazName>
    <Balance>1560.0000</Balance>
    <LastRead>84.00</LastRead>
    <UserGuid>00000000-0000-0000-0000-000000000000</UserGuid>
  </ViewReads>
</ArrayOfViewReads>
''';

class ViewReads {
  final String counterAccount;
  final String parentCounter;
  final String counterGuid;
  final int counterID;
  final String accountName;
  final String areaName;
  final String addressDesc;
  final String phone;
  final String fazName;
  final double balance;
  final double lastRead;
  final String userGuid;

  ViewReads(
      this.counterAccount,
      this.parentCounter,
      this.counterGuid,
      this.counterID,
      this.accountName,
      this.areaName,
      this.addressDesc,
      this.phone,
      this.fazName,
      this.balance,
      this.lastRead,
      this.userGuid);

  factory ViewReads.fromXmlElement(XmlElement xmlElement) => ViewReads(
      xmlElement.findElements('CounterAccount').single.text,
      xmlElement.findElements('ParentCounter').single.text,
      xmlElement.findElements('CounterGuid').single.text,
      int.parse(xmlElement.findElements('CounterID').single.text),
      xmlElement.findElements('AccountName').single.text,
      xmlElement.findElements('AreaName').single.text,
      xmlElement.findElements('AddressDesc').single.text,
      xmlElement.findElements('Phone').single.text,
      xmlElement.findElements('FazName').single.text,
      double.parse(xmlElement.findElements('Balance').single.text),
      double.parse(xmlElement.findElements('LastRead').single.text),
      _firstTextOrNull(xmlElement.findElements('UserGuid')));

  static String _firstTextOrNull(Iterable<XmlElement> xmlElements) =>
      xmlElements.isEmpty ? null : xmlElements.single.text;

  @override
  String toString() {
    return 'ViewReads{counterAccount: $counterAccount, '
        'parentCounter: $parentCounter, '
        'counterGuid: $counterGuid, '
        'counterID: $counterID, '
        'accountName: $accountName, '
        'areaName: $areaName, '
        'addressDesc: $addressDesc, '
        'phone: $phone, '
        'fazName: $fazName, '
        'balance: $balance, '
        'lastRead: $lastRead, '
        'userGuid: $userGuid}';
  }
}

void main() {
  final document = XmlDocument.parse(xml);

  final listOfViewReads = document
      .findAllElements('ViewReads')
      .map((xmlElement) => ViewReads.fromXmlElement(xmlElement))
      .toList();

  listOfViewReads.forEach(print);
  // ViewReads{counterAccount: 7e7386db-ae4e-47f8-a473-8a5534b8b421, parentCounter: 00000000-0000-0000-0000-000000000000, counterGuid: 88165f87-113b-4503-bd95-01cfc778cf0e, counterID: 354, accountName: يحي العامر, areaName: مربع الشارع العام  وغزه, addressDesc: منطقه الوء الاخضر, phone: 771707009, fazName: سنجل فاز, balance: 865.0, lastRead: 7332.0, userGuid: null}
  // ViewReads{counterAccount: 46e2bacc-644f-47fb-abe7-6589f880667e, parentCounter: 00000000-0000-0000-0000-000000000000, counterGuid: 2d2f1a40-9dcf-4a3c-9fd2-081d3be83aaa, counterID: 2052, accountName: كمال محمد علي ثامر, areaName: مربع الشارع العام  وغزه, addressDesc: خلف كوافير السلطانه, phone: 771363922, fazName: سنجل فاز, balance: 1560.0, lastRead: 84.0, userGuid: 00000000-0000-0000-0000-000000000000}
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将VueLoaderPlugin导入到TypeScript文件?

如何将OVA文件导入到proxmox

如何将Excel文件(XLSX)导入到mongoDB

如何将本地文件导入到colab

如何将AvroKeyValueOutputFormat文件导入到蜂巢中?

如何将输出从R导入到Excel文件

如何将各个模块中的选择对象导入到 __init__.py 中?

(Ruby on Rails)如何将一些数据从xml导入到关系表中?

如何将本地HTML文件导入到Swift Playground Live View?

如何将csv文件导入到mysql表中并自动递增

如何将数据从 JSON 文件导入到嵌入中?

如何将.txt文件中的“高级格式”滑块问题导入到Qualtrics中?

如何将第三方库导入到特定文件夹

如何将Objective-C文件添加/导入到快速应用程序

如何将CSV文件导入到SQLite的第一行除外?

如何将具有现有文件的本地项目导入到“源代码树”

如何将obj和mtl文件导入到three.js

如何将导入到python中的数据从csv文件转换为时间序列?

如何将 Google Drive 子文件夹 URL 导入到 Google Sheet

如何将数组从.txt文件导入到numpy数组?

如何将数据从txt文件导入到MySQL数据库

如何将这种格式化的txt文件导入到SQL Server表中

如何将json文件内容作为文档导入到mongodb中?

如何将备份文件大量导入到Postgresql数据库

如何将变量从主操作面板导入到类文件中?

Rails –如何将文件从本地主机导入到Heroku数据库

如何将一张excel文件导入到access数据库中?

如何将生成的 html 文件导入到离子框架项目中?

如何将原始时间数据从 .xlsx 文件导入到 MATLAB