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>

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.