Danh mục

Using the XML Class

Số trang: 17      Loại file: pdf      Dung lượng: 34.30 KB      Lượt xem: 10      Lượt tải: 0    
Jamona

Hỗ trợ phí lưu trữ khi tải xuống: 9,000 VND Tải xuống file đầy đủ (17 trang) 0

Báo xấu

Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Sử dụng Class XML Đó là thời gian để bắt đầu sử dụng một số XML! Gần như tất cả mọi thứ bạn làm với XML trong Flash bao gồm các lớp học XML và thuộc một trong các loại sau: định dạng XML, phân tích cú pháp XML (chiết xuất thông tin), bốc XML, hoặc gửi XML.
Nội dung trích xuất từ tài liệu:
Using the XML Class < Day Day Up >Using the XML ClassIts time to start using some XML! Nearly everything you do with XML in Flash involvesthe XML class and falls into one of the following categories: formatting XML, parsingXML (extracting the information), loading XML, or sending XML. With the XML class,you can load XML from an external source such as a static file or a server-side script.After an XML document is loaded, you can access its information using the methods andproperties of the XML class. You can also use the methods and properties of the XMLclass to create your own XML document. After this document is created, you can use it ina Flash movie or send it out to a server-side script. This section covers the ActionScriptyou need to accomplish these goals.Formatting XMLThe XML class in Flash has several methods, most of which you can use to create andformat XML documents. The truth is, though, youre unlikely to employ them becausetheyre difficult to use—and theres a better way. Well show you how to create a stringand then convert it into an XML object, a much easier (and more common) way offormatting XML objects.To create an XML object in Flash, you must use the XML class constructor. Heres howyou would create an empty XML object:var myXML:XML = new XML();To populate the object with XML-formatted data when its created, you can pass (insidethe parentheses of the constructor) the name of a variable that holds an XML-formattedstring or another XML object.Suppose you want to create the following XML document in Flash: Kelly Makar Free MakarYou would do two things: 1. Create the document as a string. 2. Convert the string to an XML object by using the XML class constructor new XML().Heres an example:var myString:String = Kelly MakarFree Makar;var myXML:XML = new XML(myString);This code creates the XML document as a string and converts it to an XML object calledmyXML. This object can then be sent to the server using the send-related methodsdescribed later in this section of the lesson.Parsing XMLThe word parse simply means to analyze something or break it down into its parts. Whensomeone speaks of writing a script to parse an XML document, theyre talking aboutwriting a script that extracts information from that XML document. The XML class hasmany properties to help you do this. Well use the XML object just discussed, myXML,to illustrate the use of a few of the most common properties.firstChild: This property points to the first node in the tree structure. For example:myXML.firstChild.firstChildreturnsKelly MakarThe first child node of the XML document is the root node (MyFriends), and the rootnodes first child is Name, as shown.childNodes: This property returns an array of the child nodes at any given point in thetree structure. For example:var myArray:Array = myXML.firstChild.childNodesHere, myArray contains two elements whose values are the same as those of the twoName nodes.nextSibling: This property points to the next node in the same level of the tree structure.Thus,myXML.firstChild.firstChild.nextSiblingreturns:Free Makarattributes: This property returns an associative array of attribute names. For example:myXML.firstChild.firstChild.nextSibling.attributes.Genderreturns:maleThese examples represent the most commonly used properties of the XML object; otherswork in the same way, referencing different parts of the tree structure.Loading XMLTypically, youll only work with XML in Flash when youre loading or sending out theXML. To load XML from a remote source, do the following: 1. Create an XML object. 2. Use the load() method of the XML object to load XML-formatted data from an external source. For example: 3. 4. var myXML:XML = new XML(); 5. 6. myXML.load(http://somedomain.com/info.xml); 7.Although in this example the document being loaded is a static XML file, it doesnt haveto be. You can also point to an ASP page (or another scripted page) whose result is anXML document.To determine when the XML has finished loading into an object, you use the onLoadevent available to the XML object. You can define this event to call a function when thedocument is finished loading. Consider the following example:function init () { //parse script here}var myXML:XML = new XML();myXML.onLoad = init;myXML.load(http://somedomain.com/info.xml);As the next-to-last line shows, when the XML document is finished loading, the init()function will be called. In the init() function, you write special code to interpret the XML.For example, if youre expecting an XML-formatted address book, you write somespecial code to walk the XML nodes, pulling out the data within them. Well show somesimple parsing examples later in this lesson.Sending XMLThe XML class allows you to send XML to a URL. It also enables you to send XML andload the resulting document simultaneously.To send XML to a URL, use the send() method and specify a destination URL:var myXML:XML = new XML(Hi!);myXML.send(http://somedomain.com/somedestination.asp);To send XML and receive a response, all in one shot, use the sendAndLoad() method ofthe XML object. With this method, you must specify an XML object whose contents youwant to send, a URL in which to send the XML document, and an XML object in whichto receive the response. As with the load() example described earlier, you must define anonLoad event to handle the loaded XML. Heres an example:var URL:String = http://www.myDomain.com/UserLogin.asp;function init () { trace(objToReceive);}var xmlToSend:String =Jobemhayes;var objToSend:XML = new XML(xmlToSend);var objToReceive:XML = new XML();objToReceive.onLoad = init;objToSend.sendAndLoad(URL, objToReceive);Th ...

Tài liệu được xem nhiều: