Asked of all Flex coders: What is the fastest way to read large XML data files and view them in a DataGrid? The four methods used and their performance on a machine ... Source: http://philflash.inway .com / dgperf / index.html
| Method 1 (classical) knots, with XMLConnector Diagram and Databinding | Method 2 Attributes, with XMLConnector Schematic and DataBinding | Method 3 attributes, parse XML and Dataprovider | Method 4 Attributes, parse XML and items | |
| Flash Player 7 | 12 388 ms | 11 963 ms | 2 930 ms | 442 ms |
| Flash Player 8 | 9 365 ms | 7 569 ms | 1 858 ms | 314 ms |
The detailed problem
In many users complain about performance flex to read files XML.Voici classification of 4 methods:
Method 1
To represent the data, we use nodes. A client is represented as follows: <client><nom>Deschamps</nom><prenom>Amandine</prenom><ville>Caluire-Et-Cuire</ville><age>43</age><ca>2173.7</ca></client> For Flash, we use a XMLConnector to read the file (using a diagram), a model for the DataSet and a DataGrid to display data. It uses data link (DataBinding) to bind the 3 éléments.C 'method is the "classic" you find in many tutorials.
Why this method is slow?
The XML file has a size of 315 kb. When using a Schema XMLConnector, Flash uses an XPath to access données.Le file is large, the XPath is slow: This is the longest. It comes to 12.5 seconds.
Method 2
The idea of this method is to use attributes (instead of nodes) and see the impact on customer performances.Un is represented as follows: <client nom="Deschamps" prenom="Amandine" ville="Caluire-Et-Cuire" age="43" ca="2173.7"/> For Flash, we use the same method as in Method 1: a XMLConnector to read the file (by importing the schema), as a DataSet model and a DataGrid to display data. It uses data link (DataBinding) to bind the 3 elements.
Why this method is faster than method 1?
The XPath is "fast" to access an attribute (for a node). Note also that the XML is more "small" (164 kb instead of 315 kb). Lasts about 12 seconds ...
Method 3
To represent the data, we use attributs.L idea of this method is not to use the layout of XMLConnector (which uses an XPath), but the parser manually XML.Pour this, we read the "quick" XML : it uses a while loop with nextSibling. To encode the numbers, we use Number (not parseInt and parseFloat which are slower). There are no more databinding between XMLConnector and Dataset. It uses the Dataprovider of the DataSet to bind the result of parsing XML.
Why this method is faster than Method 2?
It performs manually parsing XML. It no longer uses the XPath (and not using the Schema of XMLConnector). As against, it must encoder attributes. In most XML data, there are strings and numbers. The encoding is very fast. The test lasts 3 seconds.
Method 4
With method 3, we know very quickly read the XML. The idea of this method is whether they can improve the data link (using the method in Dataprovider 3). If you look at the documentation of the DataSet, there are two methods to associate data to a DataSet: the method Dataprovider and method items. What is the difference between items and Dataprovider? documentation Flash MX 2004, Flash 8 LiveDocs or does not give too many details. We must therefore look at the sources of the DataSet: In Flash 8: Macromedia \ Flash 8 \ en \ First Run \ Classes \ mx \ data \ components \ DataSet. Asen fact, the method Dataprovider performs type conversions if there is a Schema ( in the method internalAddItem). The items method makes a direct link (without conversion / check type). As we have already made the conversion, it uses the method items. (In Method 3, the type conversion was not necessary). The code is as follows:
client_con.addEventListener("result", Delegate.create(this, doParseData));//function doParseData():Void {var dataXML:XML = client_con.results;var resultArray:Array = [];var mainNode = dataXML.firstChild;var aNode:XMLNode = mainNode.firstChild;while (aNode) {var obj = new Object();for (var attribute:String in aNode.attributes) {if (attribute == “age” || attribute == “ca”) {obj[attribute] = Number(aNode.attributes[attribute]);} else {obj[attribute] = aNode.attributes[attribute];}}resultArray.push(obj);aNode = aNode.nextSibling;}// — use items (and not dataProvider)client_ds.items = resultArray;} Here is a method to move from 12.5 seconds to less than 0.5 seconds to read a file 2 000 recordings!
If you come for the first time on Flex-info.fr, you can subscribe to the RSS feed. Thank you for your visit!




















June 14th, 2008 at 13 h 04 min
Hi
I see that it is a good method, but would you put in place in a flex project. Moi je n'y arrive pas!