Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.3k views
in Technique[技术] by (71.8m points)

flutter - How to import xml file to dart object

I'm trying to import an xml file exported by c# XmlSerializer to dart object using xml package but all my tries of importing failed.

<?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>

And here is my importing code from dart using fromJson() method that generated by VSCode:

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

And this is the error after I tried running the code:

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is an example of how you can parse this XML using the xml package from: 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}
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...