Saturday, September 1, 2007

Slogans On Toothpaste

Flash and XML



In this tutorial does not present a basis for XML'a jednynie his support in Flash. Therefore, if someone does not know this technology it is worth to know its basics.
Along with Flash 5, there are two new classes of ActionScript. The first of them, thanks to which it is possible to load an XML document and performing some surgery on it is called XML . The second named XMLSocket enables communication via sockets (socket) between Flash and a server. In Flash MX added property ignoreWhite , contentType and methods getBytesLoaded , getBytesTotal XML object and event and onData objects and XML XMLSocket ( ingnoreWhite, contentType and onData were also implemented in later versions of Flash Player 5).


Now a few words about how the processing of the document in Flash. When loading the object XML document is parsed (analyzed its syntax) and it creates a tree representation. The parser only checks whether the document is properly constructed (well-formed), but does not check its compliance with the DTD (given that the the document was a declaration of a DTD), ie whether it is valid (valid).
tree is built from the nodes, which can include the tags and text. XML declaration, document type, DTD, comments are not included in its composition. Tags are nodes of type 1, have a name, have no value, the text is a node of type 3 has no name, has the value. Parser for creating the tree may or may not, depending on the value of property ignoreWhite , include white space between tags or tag of the initial and final inception. If the value of this property is false, these characters are taken into account and are created with one type 3 nodes



As was shown above example, the tags are named "book" and "author", there are signs of new lines and spaces. You may have created a knot of type 3, which will be located at the same level in the tree tags as "author" and "title". These nodes are insignificant and are usually undesirable elements. In Flash MX can easily be eliminated by assigning ownership ignoreWhite true value , before loading the document into an XML object . In Flash 5 the situation is somewhat worse because the property ignoreWhite has been implemented only since version 5.0.41.0 of Flash Player. The problem can be solved by editing the XML document and remove all unnecessary white space, or programmatically using ActionScript (it's a bit difficult to write code and slows down the script). You can also use a ready solution. These are two functions stripWhitespaceTraverse and stripWhitespaceDoublePass written by Colin Moock'a. They remove the xml object as an argument forwarded them all superfluous text nodes containing only whitespace. They can be found at the author www.moock.org . Another solution to this problem, by overriding the method parseXML , proposes Branden Hall. Source XMLNitro called and can be found at chattyfig.figleaf.com .


Below is the code with which we can easily find out which version of Flash Player work (examples: xml_1_1.fla for the Flash MX xml_1_2.fla 5):


   version =   getVersion (); 
temp = version. split (", ");
MajorVersion = temp [0]. substr (temp [0]. length -1.1); / / Major version
MinorVersion = temp [2]; / / minor version

With getVersion we uzykać information operating system and version of Flash Player. The variables MajorVersion and MinorVersion are laid major version and minor version of the player, shelled from a string returned by the function getVersion . If you start up the presentation in Flash 5 using Ctrl + Enter these properties will be equal to 5 and 30, in the case of Flash MX will be a 6 and 21 In the case of a web browser will be different and probably will be the latest version. (This happens in the case of IE, flash plugin is installed mostly "silent" and automatically, in case other have to install manually. The latest version of the player can always be downloaded from the manufacturer ). The distinction version will allow us to apply the appropriate script. This is important if we want to maintain compatibility with older versions playerów and also faster on the latest.


time to return to read the document. Here's how the steps are as under which the file is fully loaded with an XML document:



  1. Define the function performed after the operation load of the document.

  2. Create an instance of class XML.

  3. Assigning property values \u200b\u200b ignoreWhite (default is false ).

  4. Override default method onLoad .

  5. load data into an XML object .


Ad. 1. Function definition should look like this:


   function funcname (nazwa_argumentu) {
/ / function body}


nazwa_argumentu argument passed when the function call is of type boolean and assumes a value of true load the document if the operation succeeded, or false
in the opposite case. You can define instructions to be executed automatically immediately after loading the document.


   loaddata function (ok) {
if (ok) {
plainout = " file was loaded correctly. "
} else {
plainout = " errors occurred when loading a file! ";

}}

In this example, defined a function called loaddata . Her sentence is limited to display in the dynamic information on whether the file was loaded or not. To this end, the argument passed is checked
functions and depending on its variable plainout is assigned a message displayed in the dynamic field of the output.


Ad. 2. Obietku XML class instance is created with the help of the constructor new XML () . Is obtained when an empty object, in this example:


  xmldoc = new XML    (); 

You can also use a different syntax new XML (data) where data are chain-forming document XML.


  xmDoc = new XML    (   John Doe   gather chestnuts    "); 

using this syntax is an object in kórym is immediately created a tree. We have to reckon with the fact that the document would be parsed with the white space (ownership ignoreWhite default or false) and that you can not use the function performed automatically after loading the document.


Ad. 3. The property ignoreWhite already mentioned. You can assign it a value until you create an XML object to have an impact on the construction of a tree it must be done before parsing document.


Ad. 4. Overriding methods onLoad done by assigning the function name defined in point. 1 (the default implementation of the method is inactive).


Ad. 5. The document can be loaded by the method load (url) where url is the address pointing to an XML document. If you already have an object from a tree, it will be replaced.


In this example, all the points except the first are in the cage first. Load the document itself is done by pressing the "load" and calling the same function loadFile :


 ... 

xmldoc = new XML (); / / Create an XML class called xmldoc
xmldoc. ignoreWhite = true; / / Ignore whitespace unnecessary
xmldoc. onLoad = loaddata; / / Assign function

...

function loadFile () {
if (infile == null ) {/ / If no file name
out = " No file name! "
} else {
xmldoc. load (infile); / / Load the file

}}

rest of the buttons include call other functions, as defined in the first frame. Here is how to present such exercises:



In the "File Name" enter the name of one of the files to spis_5.xml spis_1.xml. These files are different code page. One of the tags contains the text of the form: Polish characters on the keyboard: żźćńąśłęóŻŹĆŃĄŚŁĘÓ . You can see how the player will display them for different code pages:



  • spis_1.xml UTF-8

  • spis_2.xml ISO-8859-2

  • spis_3.xml Windows-1250

  • spis_4.xml Macca

  • spis_5.xml UTF-16


After reading the file and use the full object xmldoc you're ready to check his property. The results are presented in two dynamic fields: the upper "plainout" is a simple text box to display a simple
text, the bottom "htmlout" is a field in which they interpret certain HTML tags (activated option "render text as html".)
keys and plugged in to their functions:


first Button memorized? ". Is contained in the function call, which, under the ownership of loaded checks whether the object has been loaded document. If loaded is equal true to say that, yes.


   isLoaded function () {if 
(xmlDoc. loaded )
plainout = " Yes. "
else
plainout = " No. ";}


2," XML declaration "- read property values \u200b\u200b xmlDecl . If you loaded the xml document contains a declaration of thanks for this property can be read, it may be, for example .


   showXMLDecl function () {if 
(xmlDoc. xmlDecl ! = null )
plainout = xmldoc. xmlDecl ;
else
plainout = " no";}


third "DTD declaration - if the document contains the document type declaration with a DTD is Property docTypeDecl it is possible to read it. This figure includes (of course, if the document contains a) a public identifier, system, and an internal subset of declarations of individuals.


   showTypeDecl function () {if 
(xmlDoc. docTypeDecl ! = Null )
plainout = xmldoc. docTypeDecl ;
else
plainout = " no";}


4th "Status" - status property holds the number indicating the result of parsing. If a document is properly constructed is zero, in the case of an error is a negative number. The following function uses the manual switch to check the status of property values \u200b\u200b and on this basis, the deregistration of the appropriate message. For older versions of player 5 may be necessary to use an if else instead of the switch.


   showStatus function () {if 
(xmlDoc. status == null )
return;
switch (xmlDoc. status) {case
0 :
plainout = " document is properly constructed (well-formed). "
break;
case -2:
plainout =" Incorrect completion of a CDATA section. "
break;
case -3:
plainout =" Invalid end XML declaration. "
break;
case -4:
plainout =" Invalid end DOCTYPE declaration. "
break;
case -5:
plainout =" Incorrect ended comment. "
break ;
case -6:
plainout = "Invalid form of an XML element. "
break;
case -7:
plainout = " Out of memory. "
break;
case -8:
plainout = " Improperly completed attribute value. "
break;
case -9:
plainout = " No end tag for one of the elements. "
break ;
case -10:
plainout = " encountered tag The final, which does not correspond to any start tag. "

}}

fifth" tree "- this button provides a call to a function that traverses the nodes of displaying their name or value.


   traverseTree function () {
htmlout = "";
traverse (xmlDoc. firstChild, 0);}


playerów code for the version below, 5.0.41.0 (file xml_1_2.fla):


   traverseTree function () {
htmlout = "";
root = xmldoc. firstChild;
while (root. nodeType! = 1) {root = root
. nextSibling; / / nextSibling next node on the same level
}
traverse (root, 0);}


difference is that, as I mentioned at the beginning, that the earlier version of Player 5 when creating the tree and nodes are taken into account all the whitespace. Arise in this way, unwanted text nodes. To get to the root element of the tree, use the firstChild property (ie the first child or the child, if you will) of an object xmldoc
. That's enough for newer playerów or when the document starts right from the root element. For older playerów and if your document contains statements, comments, whitespace before the root element must be "shell" of them. At the top level nodes are created if type 3, or text and a node of type 1 or just the principal. With the while loop is made to assign the next adjacent node (property nextSibling ) to the time at which Type one of them will be first, equal Next the root node is transmitted traverse function, which it is mixed with the tree.


   traverse function (obj, space) {var 
indent = "";
for ( var i = 1; and <=space; i++) {
indent + = " ";
}
if (vol. nodeType == 3) { / / check node type if the type 3, or a text
/ / Displays the values \u200b\u200b
htmlout + = indent + "+ vol. nodeValue + "
"

} else if (v nodeType == 1) { / / if node type is a trench or a tag (element)
htmlout + = indent + " " + vol. nodeName + " " / / print node name
for (var attrib in vol. attributes) { / / and the attribute if it exists
htmlout + = ' "+ attrib +" = "+ vol. attributes [attrib] + " ";}

htmlout + = '
";}

if (v firstChild! = null) {
traverse (v firstChild , + + space); / / recursion to recall the same function for the first child node
}
if (v nextSibling! = null) {
traverse (v nextSibling , space); / / recursion, the call feature for the next node on the same level

}}

nodes are objects for which you can use certain methods and properties of the objects of the class of XML. Here are some of the properties, which make it possible to move around the tree:



  • attributes - returns an associative array containing the names of attributes and their values \u200b\u200b

  • childNodes - returns an array containing references to child nodes

  • firstChild - contains a reference the first child node

  • lastChild - a reference to the last child node

  • nextSibling - contains a reference to an adjacent node, or to the next node on the same level

  • previousSibling - contains a reference to a neighboring node, to the previous node on the same level

  • nodeName - returns the name tag or null if it is a text node

  • nodeValue - returns a text node or text, or null if it is a tag

  • nodeType - returns a number indicating the type of node: one is an element or tag, 3 is a text node

  • parentNode - the parent returns (each node can have only one parent)

  • hasChildNodes - the method returns true if the node has children)


text box with the html option in the player against six white space behaves just like a browser, such as contrast, interprets it in the player 5 displays all the whitespace and hence does not interpret difference in the code for examples.
result is displayed in this field is only to obtain a different font color for each node. Variables space, indent responsible for the proper indentation of nodes at different levels (something lame but it works so okryjmy a veil of silence:) So, after entering the function is displayed if the node is a text node, or its name if it is tag.
Next followed a recursive function call if the node has a child or sibling.
Property attributes returns an associative array pair: the name of the attribute and its value. For the value of each attribute can be reached in two ways:


  / / as the property of the object attributes 
. Nazwa_atrybutu
/ / or as elements in an array
attrubutes [" nazwa_atrybutu " ]

Read the entire associative array allows us to design:


   for (var   key  in  array) 
{/ / key - contains the name, array [key] - contains the value
}

6th "String" - a text representation of the object xmldoc - toString method . Showing is virtually the same as the xml file contains. One note after re-reading the document to the previous declarations of the object are preserved somewhere, although the new tree is created. If you want to avoid this, re-create the object and load the file to it.


   showString function () {
plainout = xmldoc. toString ();}


second issue in the player MX toString method works a little differently than in the player 5th The change is noticeable in CDATA sections, characters such as < > & '"are converted to equivalent ie \u0026lt;> &'" It should be remembered so that the value of text nodes are always referenced using the nodeValue or overwrite method toString :


  XMLNode .   prototype.  toString  =   function () {return 
this . nodeValue;}



Encoding For encoding was a big change between 5 and MX because it supports Unicode (UTF-16LE, UTF-16BE, and UTF-8). For Flash 5 text variables in the swf files were stored in the code latin1 (ISO-8859-1), whereas in the case of MX are converted to Unicode (this raises the problem as text variables, using the example of Polish characters, recorded in the swf MX 's are incorrectly read by the player in version 5). Similarly, the data read-off from EXTERNAL sources. Flash Player 5 treated all of them if they were in the code page of the operating system while the MX player treats them (by default) if they were in Unicode. To change this to use the statement:


   system. UseCodePage =   true; 

player 6

Then when reading external data will behave exactly like the 5th However, the application itself Flash MX for the read data command # include assumes that the files are in the code page of the operating system. Therefore, to tell her that the data is in Unicode should be the first line in the file put switchable recording, eg


//!-- UTF8

In the case of 5 when reading data from external sources on the screen to see the flower stalk and bushes had not used the code page of the data suitable for the system, eg Windows xml file should be in the code page system that is Windows-1250 for maca in the code page, etc. In the case of Macca MX sufficient data to be stored in Unicode, and should display correctly on all systems (provided, of course, choose the font, having tails). In the script, the values \u200b\u200bof text variables can be used Unicode encoding Polish letters or in the form of uNNNN . For example, these characters are used in the chain attributed to the variable plainout versus loaddata .



Letter Unicode
uNNNN
& # xNNNN;

Unicode & # NNN;
Letter Unicode
uNNNN
& # xNNNN;

Unicode & # NNN;
hex
decimal
Ą 0104 260 ń 0144 324
ą 0105 261 Ó 00D3 211
Ć 0106 262 ó 00F3 243
ć 0107 263 Ś 015A 346
Ę 0118 280 ś 015B 347
ę 0119 281 Ź 0179 377
Ł 0141 321 ź 017A 378
ł 0142 322 Ż 017B 379
Ń 0143 323 ż 017C 380

Character Entities


Flash does not include the DTD so that all definitions of units are not taken into account, and appeal to them in the xml document are treated as plain text. Are interpreted predefined character entities (& '> \u0026lt;') and numbered units are not interpreted correctly (see the table in the form of Unicode characters & # & # NNN and xNNNN). These units are also called entities see znakowymi, easily displayed by the browser, EKO, and converted to Polish letters. So maybe someday he would do it and Flash.


Useful links

0 comments:

Post a Comment