Example: var myXML:XML = new XML ; myXML.onLoad = function { status XML.status property public status : Number This property automatically sets and returns a numeric value that indicates
Trang 1The trace would be
loaded (XML.loaded property)
public loaded : Boolean
This property indicates whether the XML document has been successfully loaded
Example:
var myXML:XML = new XML ();
myXML.onLoad = function (){
status (XML.status property)
public status : Number
This property automatically sets and returns a numeric value that indicates whether an XML ument was successfully parsed into an XML object or whether an error occurred because of a mal-formed XML document
doc-Example:
var myXML:XML = new XML ();
myXML.onLoad = function (){
trace (this.status);
};
myXML.load ("xml_files/malformed.xml");
Trang 2The trace would give - 6, because of a malformed node ( bath)
● – 0: No error; parse was completed successfully
● –2: A CDATA section was not properly terminated
● –3: The XML declaration was not properly terminated
● – 4: The DOCTYPE declaration was not properly terminated
● –5: A comment was not properly terminated
● – 6: An XML element was malformed
● –7: Out of memory
● – 8: An attribute value was not properly terminated
● – 9: A start-tag was not matched with an end-tag
● –10: An end-tag was encountered without a matching start-tag
xmlDecl (XML.xmlDecl property)
public xmlDecl : StringThis is a string that specifies information about an XML document To parse an XML document
in Flash a declaration is not required
Example:
var myXML:XML = new XML ();
myXML.xmlDecl = "<?xml version=\"1.0\”? 8\"?>";
Example:
var myXML:XML = new XML ();
myXML.onData = function (src:String){
trace (src);
Trang 3var myXML:XML = new XML ();
myXML.onHTTPStatus = function (httpStatus:Number){
this.httpStatus = httpStatus;
};
myXML.onLoad = function (){
trace ("httpStatus: " + this.httpStatus);
};
myXML.load("http://www.flashscript.biz/MX2004/xml2004_tutorial/xml_tutorial_1.xml");
Trang 4var myXML:XML = new XML ();
myXML.onLoad = function (success:Boolean){
trace (success);
if (success){
trace (this.status);
}};
Trang 5http://www.martijndevisser.com/blog/article/using-http-authorization-headers, where the Header
is changed to call a file protected by a username and password This is used in connection withXML.sendAndLoad( )
Example:
my_xml.addRequestHeader("Action", "'the_action'");
createElement (XML.createElement method)
public createElement(name:String) : XMLNode
This will create a new XML node and is used with appendChild( ) The parameter name will setthe node name
Example:
var myXML:XML = new XML ();
var element1:XMLNode = myXML.createElement ("house");
var element2:XMLNode = myXML.createElement ("bedroom");myXML.appendChild (element1);
element1.appendChild (element2);
trace (myXML);
will trace
<house><bedroom /></house>
createTextNode (XML.createTextNode method)
public createTextNode(value:String) : XMLNode
This method will create a new text node and is used with appendChild( ) The parameter will setthe node value
Example:
var myXML:XML = new XML ();
var element1:XMLNode = myXML.createElement ("house");
var element2:XMLNode = myXML.createElement ("bedroom");myXML.appendChild (element1);
Trang 6getBytesLoaded (XML.getBytesLoaded method)
public getBytesLoaded( ) : NumberThe number of bytes loaded will be returned
Example:
var myXML:XML = new XML ();
_root.onEnterFrame = function (){
var bytes:Number = myXML.getBytesLoaded ();
getBytesTotal (XML.getBytesTotal method)
public getBytesTotal( ) : NumberThis method returns the number of bytes of the XML document
Example:
var myXML:XML = new XML ();
_root.onEnterFrame = function (){
var bytes:Number = myXML.getBytesTotal ();
load (XML.load method)
public load(url:String) : BooleanThis is a method to load an XML document The parameter is a string that is the URL for anXML document When loading is initiated the XML property loaded is set to false, and when
Trang 7the XML document is completely downloaded from the server, the property loaded is set totrue.
Example:
var myXML:XML = new XML ();
myXML.onLoad = function (){
trace ("After: " + this.loaded);
parseXML (XML.parseXML method)
public parseXML(value:String) : Void
This method parses the XML text specified in the parameter In the example below, withoutparseXML(xml_str), the trace would give “undefined”
send (XML.send method)
public send(url:String, [target:String], [method:String]) : Boolean
This encodes the specified XML object into an XML document and sends it to the specified URL,such as a php file
Example:
var my_xml:XML = new XML ();
my_xml.contentType = "text/xml";
Trang 8var newNode:XMLNode = my_xml.createElement ("login");
newNode.attributes["phone"] = "555-922-4567";
newNode.attributes["email"] = "foe@foemail.com";
my_xml.appendChild (newNode);
my_xml.send ("xml_parser.php", "_blank");
sendAndLoad (XML.sendAndLoad method)
public sendAndLoad(url:String, resultXML:XML) : VoidThis method encodes the specific XML object into an XML document, sends it to the URL, anddownloads the response from the server as specified in resultsXml
trace (msg);
}else{var msg:String = this.firstChild.attributes.error;
trace (msg);
}};
var my_xml:XML = new XML ("<login email=\"foe@foemail.com\"phone=\[E22]"555-922-4567\" />");
my_xml.contentType = "text/xml";
my_xml.sendAndLoad ("xml_parser.php", resultsXml);
The Object Class
The XML class has properties and methods inherited from other classes such as the Object class
● addProperty (Object.addProperty method)
● hasOwnProperty (Object.hasOwnProperty method)
● isPropertyEnumerable (Object.isPropertyEnumerable method)
● isPrototypeOf (Object.isPrototypeOf method)
Trang 9● registerClass (Object.registerClass method)
● toString (Object.toString method)
● unwatch (Object.unwatch method)
● valueOf (Object.valueOf method)
● watch (Object.watch method)
The XMLNode Class: Properties
Some of the properties are read-only, as indicated
attributes (XMLNode.attributes property)
public attributes : Object
This is an object used to access attributes in an XML file
Example:
var my_xml:XML = new XML ();
var newNode:XMLNode = my_xml.createElement ("login");newNode.attributes["phone"] = "555-922-4567";
newNode.attributes["email"] = "foe@foemail.com";my_xml.appendChild (newNode);
trace(my_xml.firstChild.attributes.phone);
will trace
555-922-4567
childNodes (XMLNode.childNodes property)
public childNodes : Array [read-only]
This is a read-only array, which will list all child nodes
firstChild (XMLNode.firstChild property)
public firstChild : XMLNode [read-only]
This is a reference to the first child of an XML document or parent node
Trang 10lastChild (XMLNode.lastChild property)
public lastChild : XMLNode [read-only]
This is a reference to the last child of an XML document or parent node
localName (XMLNode.localName property)
public localName : String [read-only]
This is a reference to the XML node name in an XML document with namespaces
Example (the following namespace XML file will be used for all properties and methods related
to namespaces):
<?xml version="1.0" encoding="UTF-8"?>
<ag:Agency xmlns:ag="http://www.getyourownhouse.com">
<hs:Bodyxmlns:hs="http://www.getyourownhouse.com/houses">
<hs:Description>
<hs:Built text="Built in ">1990</hs:Built>
<hs:Location text="Located in ">Sacramento</hs:Location>
<hs:Price text="Price: ">$239,000</hs:Price>
</hs:Description>
</hs:Body>
</ag:Agency>
Trang 11Code in Flash movie:
var my_xml:XML = new XML ();
my_xml.ignoreWhite = true;
my_xml.onLoad = function (){
namespaceURI (XMLNode.namespaceURI property)
public namespaceURI : String [read-only]
This is a reference to the URL identifier in a namespace XML document.See above example, except:
trace (this.firstChild.namespaceURI);
will trace
http://www.getyourownhouse.com/houses
nextSibling (XMLNode.nextSibling property)
public nextSibling : XMLNode [read-only]
This is a reference to the next sibling of the first child in an array of child nodes.Example:
var my_xml:XML = new XML("<bedroom>3</bedroom><price>100,000</price>");trace (my_xml.firstChild.nextSibling);
will trace
<price>100,000</price>
nodeName (XMLNode.nodeName property)
public nodeName : String
This is a reference to the name of an XML node
Trang 12nodeValue (XMLNode.nodeValue property)
public nodeValue : StringThis is a reference to the value of an XML node
parentNode (XMLNode.parentNode property)
public parentNode : XMLNode [read-only]
This is a reference to the parent node of a child node
prefix (XMLNode.prefix property)
public prefix : String [read-only]
This is a reference to the prefix of a namespace XML document
See namespace example and change the trace line:
trace (this.firstChild.prefix);
Trang 13will trace
a g
previousSibling (XMLNode.previousSibling property)
public previousSibling : XMLNode [read-only]
This is a reference to the previous sibling of a child node
public XMLNode(type:Number, value:String)
Using the constructor and the new word, a new instance of an XML node can be created Thereare two parameters, the node type and its value Although there are 12 possibilities for nodetypes the Flash player supports only two of them, type 1, ELEMENT_NODE, and type 3,TEXT_NODE
Example:
var my_node_1:XMLNode = new XMLNode (1, "<bedroom>");
trace ("1: "+my_node_1);
var my_node_2:XMLNode = new XMLNode (3, "<bedroom>");
trace ("3: "+my_node_2); // this will now become text node
will trace
1: <<bedroom> />
3: <bedroom>
The XMLNode Class: Methods
appendChild (XMLNode.appendChild method)
public appendChild(newChild:XMLNode) : Void
This method will append a new child node to an XML document root or child
Trang 14var myXML:XML = new XML ();
var element1:XMLNode = myXML.createElement ("house");
var element2:XMLNode = myXML.createElement ("bedroom");myXML.appendChild (element1);// appends element 1 to rootelement1.appendChild (element2);// appends element 2 toelement 1 trace (myXML);
will trace
<house><bedroom /></house>
cloneNode (XMLNode.cloneNode method)
public cloneNode(deep:Boolean) : XMLNodeThis method allows you to copy nodes The parameter is either “true” or “false”
Example:
var myXML:XML = new XML ();
var element1:XMLNode = myXML.createElement ("house");
var element2:XMLNode = myXML.createElement ("bedroom");myXML.appendChild (element1);
public getNamespaceForPrefix(prefix:String) : StringThis returns the namespace URI
See namespace example and change the trace line:
trace (this.firstChild.getNamespaceForPrefix("ag"));
will trace
http://www.getyourownhouse.com
Trang 15getPrefixForNamespace (XMLNode.getPrefixForNamespace method)
public getPrefixForNamespace(nsURI:String) : String
This returns the prefix for a give namespace URI
See namespace example and change the trace line:
trace (this.firstChild.getPrefixForNamespace("http://www.getyourownhouse.com"));
will trace
a g
hasChildNodes (XMLNode.hasChildNodes method)
public hasChildNodes( ) : Boolean
This method returns true when an XML node has child nodes
insertBefore (XMLNode.insertBefore method)
public insertBefore(newChild:XMLNode, insertPoint:XMLNode) : Void
This will insert a new child node at a given insert point
Example:
var my_xml:XML = new XML ("<price>100,000</price>");var new_node:XML = new XML ("<bedroom>3</bedroom>");var insert_point:XMLNode = my_xml.firstChild;
my_xml.insertBefore (new_node, insert_point);
trace (my_xml);
will trace
<bedroom>3</bedroom><price>100,000</price>
Trang 16removeNode (XMLNode.removeNode method)
public removeNode( ) : VoidThis will remove a node specified as part of an XML document
will trace
<price>100,000</price>
toString (XMLNode.toString method)
public toString( ) : StringThis method converts an XML document or node to a string and allows string manipulations Thiscan be important for XML manipulations
Example:
var my_xml:XML = new XML("<bedroom>3</bedroom><price>100,000</price>");
var node_string:String = my_xml.toString ();
var splitted:Array = node_string.split ("3");
Trang 174 Tutorial: Creating a Universal
XML Load/onload Class
Introduction
In this chapter we will create our first class file What you will learn is how to load an XML file
We will first discuss the standard way to load an XML file However, because of problems that canoccur, we will design our own XML loading class file, which we will use throughout the book
A Regular XML Load/onLoad Script
To load an XML file we will use the load method combined with an onLoad event handler A ular class script to load and display an XML file would be something like this:
reg-class scripts.XML_regular{
private var myXML:XML;
private var myText:TextField;
private var myClip:MovieClip;
public function XML_regular (){
}public function test (myFile:String){
_level0.myClip.myText.text = String(this);
trace ("This" + this);
}}
We first define a number of variables right before the constructor, because we want these variables to
be recognized in every function These are the XML object, a text field to display the XML, and a
Trang 18MovieClip, which contains the text field Now look at the function test, which has the parametermyFile of data type String This is just another way of initiating a local variable, in this case only forthis function But do not forget to add the data type Then we write the XML-onLoad/load script.First we need to create a new instance of the XML object Since Flash MX, objects are instantiated bythe “new” word Then we make sure by setting “myXML.ignoreWhite true;” that all the whitespace in the XML file is ignored, since white space is also regarded as node Do not forget this line,because you may get strange results Next we have an onLoad event This event is executed only whenthe XML file is fully loaded We create a new function, “loadXML”, to display and, if we intend, toparse the XML once the data is loaded The last line simply initiates the loading of the XML file fromthe server The XML file is held in the String variable “myFile” The function “loadXML” contains aline that will show the whole XML file in the text field Note here that we use the word “this” to dis-play the XML, which refers to the XML object myXML This could be a problem, if we need to refer
to objects outside this function You can see what I mean if instead of “String(this)” we write
“String(myXML)”, which would give undefined You will learn how to solve this problem later
We want to display the XML data and, therefore, we convert the XML object to a string, becausetext fields display strings Now we examine the fla file (XML_regular.fla) and the script, which isneeded to actually execute the class:
import scripts.XML_regular;
var xmlTtest:XML_regular = new XML_regular ();
var myFile:String = "xml_files/combo.xml";
xmlTtest.test (myFile);
We import the class XML_regular and create an instance, xmlTest Then we create another able for the path to the XML file, which is the string variable myFile Finally we call the functiontest using the class instance
vari-Introducing the Delegate Class
As mentioned earlier, functions within an onLoad event have a narrow scope, meaning that objectsoutside this function cannot be accessed The word “this” refers to the object triggering the func-tion but not anything outside of the function This can cause a number of problems, since we oftenneed access to objects outside of this function Using the Delegate class can change the scope of thefunction In the example the Delegate class is introduced If you have Flash MX2004 you will need
to update to Flash 7.2
First you need to import this class
import mx.utils.Delegate;
Then the Delegate class is incorporated in the following way using our example above:
myXML.onLoad = Delegate.create (this, loadXML);
myXML.load (myFile);
}