|
Monday, 14 July 2008
|
HTML clipboard 1. XML: base 1 This first article will teach you the basics of handling a document XML. I invite you to visit the section XML.Pour any questions about this tutorial 1.1. Orders To load XML data, we must create an XML object. In the dictionary AS (Action Script) you can see all the properties of XML object. In this article we will use these commands to load the XML document: XML.getBytesLoaded, XML.getBytesTotal, XML.load. XML.getBytesLoaded XML.getBytesLoaded returns the number of bytes responsible for the document XML Least Flash Player 6 Syntax monCodeXML.getBytesLoaded () Example document.getBytesLoaded () XML.getBytesTotal XML.getBytesTotal refers size, in bytes, XML document specified. Least Flash Player 6 Syntax monCodeXML.getBytesTotal () Example document.getBytesTotal () XML.load XML.load load an XML document from the URL. Least Flash Player 5 Syntax monCodeXML.load (URL) Example document = new XML () document.load ( "monXML.xml") To retrieve the data we use XML.attributes. XML.attribut XML.attributes returns an associative array containing all the attributes of the XML object specified. Least Flash Player 5 Syntax monCodeXML.attributes Example document = new XML () document.load ( "monXML.xml") trace (document.firstChild.childNodes [1]. attributes.nom) 1.2. application It creates an XML document. <? xml version = "1.0"?> <root> <nouvelle numero="3" date="17/12/2003" /> </ root> It supports XML document. document = new XML ();// creation of the XML object document.ignoreWhite = true // ignore linebreaks document.load ( "essai.xml"); / / charging document To retrieve the number and date of the new, it is as follows: document.firstChild.childNodes [0]. attributes.numero document.firstChild.childNodes [0]. attributes.date Manipulation of an XML document: How many new ones? document.firstChild.childNodes.length What is the date of the new No. 1? document.firstChild.childNodes [0]. attributes.date
1.3. Going further The code below explains how preload XML data. We prechargeons data XML document. _root.document = new XML ();// creation of the XML object _root.document.ignoreWhite = true;// empty spaces are deleted _root.document.load ( "monXMl.xml ");// document loading XML _root.stop (); _root.document.onLoad = function () (/ / when loading the document is completed, the launch of the animation trace (this.firstChild.childNodes [0]. Attributes.texte); _root. play (); ) 2. XML and Database (ASP) The creation of an XML document on the fly is quite simple. In the ASP page we carry out a query that selects all the news. After we publish in the formatting as an XML file. In the example below, we create an XML document of this kind. <? xml version = "1.0"?> <root> <nouvelle date="10/02/2003" texte=" testing 1"/> </ root> Here is the code of the ASP page: <@ CodePage = 65001 Language = "VBScript"> <% 'connection to the database Set oC = Server.CreateObject ( "ADODB.Connection") = oC.Provider "Microsoft.Jet.OLEDB.4.0" oC.Open " Data Source = "& Server.MapPath ("../../ databases / ind.mdb") 'selects any new SQL = "SELECT * FROM news order by date DESC' creation of Recordset Set oRS = CreateObject (" ADODB.Recordset ") oRS.Open SQL, oC, 3.3 'creation of XML document response.write (" <xml version = "" "1.0" "?>")" response.write (<root>) 'Loop to collect all new ors.movefirst do while not ors.eof response.Write ( "<new date =" "& ors (" date ") &" "text =" "& ors (" text " ) & "" "/>") Ors.movenext Loop 'on the farm XML document response.write ( "</ root>") 'on strong connection to the bdd Set oRS = Nothing oC.Close Set oC = Nothing %> 3. XPath Flash MX 2004 introduced a new class XPathAPI. It allows you to search with simple Xpath in Flash. 3.1. Commands The class XPathAPI is not a class built in Flash Player 7. It is in a bookstore called special DataBindingClasses. Before see a concrete case, see all the expressions supported by Flash. <employees> <person gender="m">Matt</ person> <person gender="f"> Heather </ person> <person gender="m"> Tucker </ person> <person gender="f"> Apple </ person> <person gender="m"> Nate </ person> </ employees> To access the node person, one can either use an absolute path / Employees / Person either way on a person if the current node is <employees>. You can also use the wildcard (*). For example with our file: the path "/ * / person" searches for all elements <person> regardless of the parent node. The operator = is also supported. This path is clear we all employees who are women "/ employees / person [@ gender = 'f']." This path "/ employees / person [@ gender = 'f' and @ age ='30 ']" clear all employees who are women and who have 30 years. The following operators are not supported by Flash: "<", ">", "//". 3.2. Application <employees> <person gender="m"> Matt </ person> <person gender="f"> Heather </ person> <person gender="m"> Tucker </ person> <person gender="f"> Apple </ person> <person gender="m"> Nate </ person> </ employees> In our new document, we will import the library DataBindingClasses * Open: Window / Other panels / Library municipalities / Classes * Select DataBindingClasses the library and drag it into the library of your animation. * On the first frame of your animation type: import mx.xpath.XPathAPI;. The line can import class XpathApi in Flash Now that our class is imported in our animation, we will create our interface. * Place two buttons: one for men (bnt_H) and one for women (bnt_F) * Place a textarea (txt_pesonne) and the HTML option put "true" The function fill adds values of the table in the textarea Fill function (text) ( / * delete the contents of textarea * / txt_pesonne.text = "" / * on each person adds to textarea * / for (var i = 0; i <texte.length i + +) ( txt_pesonne. + text = text [i]; ) ) Can we entrust our XML file and we add all the names myXML = new XML () myXML.ignoreWhite = true; myXML.onLoad = function (success) ( if (success) ( / * define the path * / var path = "/ employees / person / * gets on everyone * / Var personNoeud = mx.xpath.XPathAPI.selectNodeList (this.firstChild Road); / * fill the textarea * / Fill (personNoeud) ) ) myXML.load ( "xpath.xml") The button bnt_F display the names of women. bnt_F.onPress = function () ( var path = "/ employees / person [@ gender = 'f']"; personNoeud var = mx.xpath.XPathAPI.selectNodeList (myXML.firstChild Road); / * on each person adds the textarea * / Fill (personNoeud) ) The button bnt_H displays his name all men bnt_H.onPress = function () ( var path = "/ employees / person [@ gender = 'm']"; personNoeud var = mx.xpath.XPathAPI.selectNodeList (myXML.firstChild Road); / * on each person adds the textarea * / Fill (personNoeud) ) Download the zip file containing the XML and fla.
|