Data Integration
Sample
(Flash MX/5; PDF)
Turbine
can read data from several sources and from several formats either
locally or from remote locations through HTTP requests. Turbine supports
the following data sources:
Reading
Variables from a Var File
This example
shows how to read several variables from a variables file. The following
MML reads the file varfile.txt and displays the read variables in <Text>
tags:
<Var
src="varfile.txt"/>
<Text pos='10,10'>var1 = {var1}</Text>
<Text pos='10,30'>var2 = {var2}</Text>
To
accomplish the same results directly from the scripting interface, we
could instead do (data1.aspx):
// load
a variables file
Turbine.LoadVar("varfile.txt");
// display
two loaded variables by using two <text> tags
Turbine.Create("<text pos='10,10'>var1 = {var1}</text>");
Turbine.Create("<text pos='10,30'>var2 = {var2}</text>");
The varfile.txt
contents are:
# this is a
comment
# this is a var definition file
var1 = "value of var1"
#
the following line is an error. Errors are ignored:
invalid line
# var names
can be optionally enclosed by {}s
{var2} = value of var2
The results
in Flash are:
Reading
a DataSet from File
This
example shows how to read a Turbine DataSet from a file:
<DataSet
src="dataset.txt" DataSet="data"/>
<Text
pos='10,{data$y.0}' color='{data$color.0}'>
row 0 url: {data$url.0}
</Text>
<Text
pos='10,{data$y.1}' color='{data$color.1}'>
row 1 url: {data$url.1}
</Text>
<Text
pos='10,{data$y.2}' color='{data$color.2}'>
row 2 url: {data$url.2}
</Text>
<Text
pos='10,{data$y.3}' color='{data$color.3}'>
row 3 url: {data$url.3}
</Text>
To accomplish
the same results directly from the scripting interface, one could
do instead (data2.aspx):
//
load a DataSet file
Turbine.LoadDataSet("dataset.txt", "data");
//
display DataSet cells by using some <text> tags
Turbine.Create("<text pos='10,{data$y.0}' color='{data$color.0}'>row
0 url: {data$url.0}</text>");
Turbine.Create("<text pos='10,{data$y.1}' color='{data$color.1}'>row
1 url: {data$url.1}</text>");
Turbine.Create("<text pos='10,{data$y.2}' color='{data$color.2}'>row
2 url: {data$url.2}</text>");
Turbine.Create("<text pos='10,{data$y.3}' color='{data$color.3}'>row
3 url: {data$url.3}</text>");
The
dataset.txt file contents are:
<date, url,
color, y>
10-2, http://www.blue-pacific.com, #ff00ff,
10
11-2, http://www.flashkit.com, #ff0000, 30
12-2, http://slashdot.org, #000000, 50
13-2, http://www.e-sheep.com, #11ff00, 70
Creating
a DataSet Manually
This
example shows how to use the component interface to create a DataSet
and to fill it with data (data3.aspx):
//
make a DataSet "manually"
Turbine.MakeDataSet("handmade_dataset", "col1,col2");
Turbine.AddDataSetRow("r0.c0,r0.c1");
Turbine.AddDataSetValue("handmade_dataset",
"col2", 0, "new value");
Turbine.AddDataSetColumn("handmade_dataset",
"col3", 0, "r0.c2");
//
display DataSet values by using three <text> tags
Turbine.Create("<text pos='10,10'>handmade_dataset$col1.0
= {handmade_dataset$col1.0}</text>");
Turbine.Create("<text pos='10,30'>handmade_dataset$col2.0
= {handmade_dataset$col2.0}</text>");
Turbine.Create("<text pos='10,50'>handmade_dataset$col3.0
= {handmade_dataset$col3.0}</text>");
The
results in Flash are:
Reading
a DataSet from an XML File
Turbine
also deals very well with XML data. This example shows how to convert
XML into a DataSet:
<DataSet
src="xml_xmldatasetfile.txt"
DataSet="book"
RowTag="chapter"
ColumnTags="pages,
title, interesting, topics">
<text pos='10,10'>book$title.0 = {book$title.0}</text>"
<text pos='10,30'>book$pages.0 = {book$pages.0}</text>"
<text pos='10,50'>book$interesting.0 = {book$interesting.0}</text>"
<text pos='10,70'>book$topics.0 = {book$topics.0}</text>"
<text pos='10,100'>book$title.1
= {book$title.1}</text>"
<text pos='10,120'>book$pages.1 = {book$pages.1}</text>"
<text pos='10,140'>book$interesting.1 = {book$interesting.1}</text>"
<text pos='10,160'>book$topics.1 = {book$topics.1}</text>"
To
accomplish the same results directly from the scripting interface,
one could do (data4.aspx):
//
load a DataSet from an XML file
Turbine.LoadDataSet("xml_xmldatasetfile.txt",
"book", "chapter", "pages,
title, interesting, topics" );
// display DataSet values with <text> tags
Turbine.Create("<text pos='10,10'>book$title.0
= {book$title.0}</text>");
Turbine.Create("<text pos='10,30'>book$pages.0
= {book$pages.0}</text>");
Turbine.Create("<text pos='10,50'>book$interesting.0
= {book$interesting.0}</text>");
Turbine.Create("<text pos='10,70'>book$topics.0
= {book$topics.0}</text>");
Turbine.Create("<text pos='10,100'>book$title.1
= {book$title.1}</text>" );
Turbine.Create("<text pos='10,120'>book$pages.1
= {book$pages.1}</text>");
Turbine.Create("<text pos='10,140'>book$interesting.1
= {book$interesting.1}</text>");
Turbine.Create("<text pos='10,160'>book$topics.1
= {book$topics.1}</text>");
The
xml_xmldatasetfile.txt file contents are:
<book>
<name>Gone
with the rain</name>
<author>Arabian Bruce Lawrence</author>
<price>$39</price>
<chapter>
<title id="1">First
Launch</title>
<pages>45</pages>
<interesting>90%</interesting>
<topics>Love, hate &
video</topics>
</chapter>
<chapter_not_valid>
<title id="1">what?</title>
<pages>45</pages>
<interesting>90%</interesting>
<topics>Love, hate &
adventure</topics>
</chapter_not_valid>
<chapter>
<title id="2">Second
Launch</title>
<pages>71</pages>
<interesting>30%</interesting>
<topics>Sex, Love &
Hate</topics>
</chapter>
</book>
T he
results in Flash are:
Reading
a DataSet from a String
And data
can also be integrated from a string. This example shows how to read
a Turbine DataSet from an XML string:
<DataSet dataSet="book"
rowTag="chapter"
columnTags="interesting=int,
title#titleattr, pages#pagesattr=pg,
topics, pages#pagesattr">
<chapter>
<title titleattr='1st launch'>First
Launch</title>
<pages pagesattr='45'/>
<interesting>90%</interesting>
<topics>Love, hate, adventure</topics>
</chapter>
</DataSet>
<Text
pos='10,10'>book$title#titleattr.0 = {book$title#titleattr.0}</Text>
<Text pos='10,30'>book$int.0 = {book$int.0}</Text>
<Text pos='10,50'>book$pg.0 = {book$pg.0}</Text>
<Text pos='10,70'>book$topics.0 = {book$topics.0}</Text>
<Text pos='10,90'>book$pages#pagesattr.0 = {book$pages#pagesattr.0}</Text>
To
accomplish the same results directly from the scripting interface,
we could instead do (data5.aspx):
//
load a DataSet from an XML string
Turbine.DataSetFromString("<chapter>
<title
titleattr='1st launch'>First Launch</title>
<pages
pagesattr='45'/>
<interesting>90%</interesting>
<topics>Love,
hate, adventure</topics>
</chapter>",
"book",
"chapter",
"interesting=int,
title#titleattr, pages#pagesattr=pg, topics, pages#pagesattr");
//
display DataSet values with <text> tags
Turbine.Create("<text pos='10,10'>book$title#titleattr.0
= {book$title#titleattr.0}</text>");
Turbine.Create("<text pos='10,30'>book$int.0
= {book$int.0}</text>");
Turbine.Create("<text pos='10,50'>book$pg.0
= {book$pg.0}</text>");
Turbine.Create("<text pos='10,70'>book$topics.0
= {book$topics.0}</text>");
Turbine.Create("<text pos='10,90'>book$pages#pagesattr.0
= {book$pages#pagesattr.0}</text>");
The results
in Flash are: