Tải bản đầy đủ (.pdf) (17 trang)

Tài liệu Using the XML Class doc

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (34.3 KB, 17 trang )



< Day Day Up >

Using the XML Class
It's time to start using some XML! Nearly everything you do with XML in Flash involves
the XML class and falls into one of the following categories: formatting XML, parsing
XML (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 and
properties of the XML class. You can also use the methods and properties of the XML
class to create your own XML document. After this document is created, you can use it in
a Flash movie or send it out to a server-side script. This section covers the ActionScript
you need to accomplish these goals.
Formatting XML
The XML class in Flash has several methods, most of which you can use to create and
format XML documents. The truth is, though, you're unlikely to employ them because
they're difficult to use—and there's a better way. We'll show you how to create a string
and then convert it into an XML object, a much easier (and more common) way of
formatting XML objects.
To create an XML object in Flash, you must use the XML class constructor. Here's how
you would create an empty XML object:

var myXML:XML = new XML();


To populate the object with XML-formatted data when it's created, you can pass (inside
the parentheses of the constructor) the name of a variable that holds an XML-formatted
string or another XML object.
Suppose you want to create the following XML document in Flash:


<MyFriends>

<Name Gender="female">Kelly Makar</Name>

<Name Gender="male">Free Makar</Name>

</MyFriends>


You 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().
Here's an example:


var myString:String = "<MyFriends> <Name Gender=\"female\">Kelly Makar</Name>
<Name

Gender=\"male\">Free Makar</Name></MyFriends>";

var myXML:XML = new XML(myString);


This code creates the XML document as a string and converts it to an XML object called
myXML. This object can then be sent to the server using the send-related methods
described later in this section of the lesson.
Parsing XML
The word parse simply means to analyze something or break it down into its parts. When
someone speaks of writing a script to parse an XML document, they're talking about

writing a script that extracts information from that XML document. The XML class has
many properties to help you do this. We'll 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.firstChild


returns

<Name Gender="female">Kelly Makar</Name>


The first child node of the XML document is the root node (MyFriends), and the root
node's first child is Name, as shown.
childNodes: This property returns an array of the child nodes at any given point in the
tree structure. For example:

var myArray:Array = myXML.firstChild.childNodes


Here, myArray contains two elements whose values are the same as those of the two
Name nodes.
nextSibling: This property points to the next node in the same level of the tree structure.
Thus,

myXML.firstChild.firstChild.nextSibling


returns:


<Name Gender="male">Free Makar</Name>


attributes: This property returns an associative array of attribute names. For example:

myXML.firstChild.firstChild.nextSibling.attributes.Gender


returns:

"male"



These examples represent the most commonly used properties of the XML object; others
work in the same way, referencing different parts of the tree structure.
Loading XML
Typically, you'll only work with XML in Flash when you're loading or sending out the
XML. 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("
7.

Although in this example the document being loaded is a static XML file, it doesn't have

to be. You can also point to an ASP page (or another scripted page) whose result is an
XML document.
To determine when the XML has finished loading into an object, you use the onLoad
event available to the XML object. You can define this event to call a function when the
document is finished loading. Consider the following example:

function init () {

//parse script here

}

var myXML:XML = new XML();

myXML.onLoad = init;

myXML.load("


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 you're expecting an XML-formatted address book, you write some
special code to walk the XML nodes, pulling out the data within them. We'll show some
simple parsing examples later in this lesson.
Sending XML
The XML class allows you to send XML to a URL. It also enables you to send XML and
load the resulting document simultaneously.
To send XML to a URL, use the send() method and specify a destination URL:

var myXML:XML = new XML("<Message><Text>Hi!</Text></Message>");


myXML.send("


To send XML and receive a response, all in one shot, use the sendAndLoad() method of
the XML object. With this method, you must specify an XML object whose contents you
want to send, a URL in which to send the XML document, and an XML object in which
to receive the response. As with the load() example described earlier, you must define an
onLoad event to handle the loaded XML. Here's an example:

var URL:String = "

function init () {

trace(objToReceive);

}

var xmlToSend:String =
"<Login><Username>Jobem</Username><Password>hayes</Password></Login>";

var objToSend:XML = new XML(xmlToSend);

var objToReceive:XML = new XML();

objToReceive.onLoad = init;

objToSend.sendAndLoad(URL, objToReceive);



This ActionScript creates an XML object (objToSend) containing login information and
then sends that information to a URL, where it waits for a response from the destination.
When the response is fully loaded into the receiving XML object (objToReceive), the init
function is called.

×