Using of Dart XML in Flutter

3,881 views
Skip to first unread message

Sandi Kastelic

unread,
Nov 28, 2018, 4:01:53 PM11/28/18
to Flutter Dev

Dear sir,

Please, I hope someone can help me with usage of Dart XML in Flutter.  I would like to use it for reading an xml file as it is bellow. So far, I can open file, get it into a string and do this in code:

 

    if (masterFile.length > 0) {

      var skus = xml.parse(masterFile);

      print(skus.toString());

      skus.findAllElements("Item");

      var reader =

          xml.XmlReader(onStartElement: (name, attributes) => print(name));

      reader.parse(masterFile);

    }

 

 

With this, I get all names for all items. But, how can I get each item one by one, together with field values, so I can write records into database one by one? Maybe you can point me to some sample?

 

Regards,

Sandi


XML sample:

 

<?xml version="1.0" standalone="yes"?>

<Items>

  <Item>

    <BAR>0000000000005       </BAR>

    <BAR_DESC>BELGIOIOSO SNKNG                                                                                    </BAR_DESC>

    <QTY>0.000</QTY>

    <PRICE>0.0000</PRICE>

  </Item>

  <Item>

    <BAR>0000000000001       </BAR>

    <BAR_DESC>MISS C WATER                                                                                        </BAR_DESC>

    <QTY>0.000</QTY>

    <PRICE>0.0000</PRICE>

  </Item>

  <Item>

    <BAR>0000000000002       </BAR>

    <BAR_DESC>MISS C TOTE BAG                                                                                     </BAR_DESC>

    <QTY>0.000</QTY>

    <PRICE>0.0000</PRICE>

  </Item>

</Items>

Message has been deleted

c.sede...@gmail.com

unread,
Dec 3, 2018, 1:51:17 PM12/3/18
to Flutter Dev
Try reading this:

https://pub.dartlang.org/packages/xml

Or as they used to say: RTFM!

EthicCoders Apps

unread,
Dec 5, 2018, 3:48:37 PM12/5/18
to sandik...@gmail.com, flutt...@googlegroups.com
main.dart file attached.... (Screenshot below) 

Simulator Screen Shot - iPhone XR - 2018-12-06 at 02.15.14.png

Simple XML parsing sneak preview... 
final sampleXml = '<?xml version="1.0" standalone="yes"?> <Items> <Item> <BAR>0000000000005 </BAR> <BAR_DESC>BELGIOIOSO SNKNG </BAR_DESC> <QTY>0.000</QTY> <PRICE>0.0000</PRICE> </Item> <Item> <BAR>0000000000001 </BAR> <BAR_DESC>MISS C WATER </BAR_DESC> <QTY>0.000</QTY> <PRICE>0.0000</PRICE> </Item> <Item> <BAR>0000000000002 </BAR> <BAR_DESC>MISS C TOTE BAG </BAR_DESC> <QTY>0.000</QTY> <PRICE>0.0000</PRICE> </Item> </Items>';
List<Item> itemsList = List();
parsing() {
var document = xml.parse(sampleXml);
//print(document.toString());
//print(document.toXmlString(pretty: true, indent: '\t'));
Iterable<xml.XmlElement> items = document.findAllElements('Item');
items.map((xml.XmlElement item) {
var bar = getValue(item.findElements("BAR"));
var bar_desc = getValue(item.findElements("BAR_DESC"));
var qty = getValue(item.findElements("QTY"));
var price = getValue(item.findElements("PRICE"));
itemsList.add(Item(bar, bar_desc, qty, price));
}).toList();
}

getValue(Iterable<xml.XmlElement> items) {
var textValue;
items.map((xml.XmlElement node) {
textValue = node.text;
}).toList();
return textValue;
}


--
You received this message because you are subscribed to the Google Groups "Flutter Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
main.dart

Sandi Kastelic

unread,
Dec 6, 2018, 3:18:54 PM12/6/18
to Flutter Dev
Thank you very much for your kind answer. It works perfect!

Best regards,
Sandi

Dne sreda, 05. december 2018 21.48.37 UTC+1 je oseba EthicCoders Apps napisala:
Reply all
Reply to author
Forward
0 new messages