Tips for Working with XML in Flash

In this section, Ill give you some tips for working with XML documents in Flash. Factors such as the structure of your XML document, using the XMLNode class, and choosing the right approach can make it much easier to create Flash XML applications.

XML file structure

As youve discovered , extracting information from the XML tree can be a lengthy process. Its common for paths to specific nodes or attributes to be long and difficult to comprehend. Look at this example:

 myXML.firstChild.childNodes[0].childNodes[0].firstChild.nodeValue; 

The path refers to the text contained within a grandchild of the root node. The use of the firstChild property and the childNodes[0] array item dont provide much of a clue to the name of the node that were interrogating.

Try to keep the structure of your XML document as shallow as possible. If you are able to limit the document to three or four levels of child elements, it will simplify the data extraction process greatly. It will also make it easier to manipulate the document tree within Flash.

Use the XMLNode class

Flash provides the XMLNode class to make it easier to work with specific locations in the document tree. You can create references to points within the XML tree using descriptive names , as shown here:

 var NameNode:XMLNode = myXML.firstChild.childNodes[0].childNodes[0]; NameNode.firstChild.nodevalue; 

The advantages of referring to NameNode are obvious. The paths to locations in the tree will be shorter, and the descriptive names will make these paths easier to understand.

Create XML with a string

If you need to create an XML document tree within Flash, you have two options: create an XML string or use the methods of the XML class. As you saw earlier in the chapter, its much quicker to create your XML tree using a string of XML information.

Validate your XML documents

Valid XML documents are well formed and meet the criteria set down in either a DTD or schema. Chapter 3 provides more information about creating DTDs and schemas.

Flash doesnt check that a document is well formed before loading it into an XML object, so its possible for Flash to load XML documents that are not well formed. You can easily check for the document by displaying the document in a web browser such as Internet Explorer or by using an XML editor. In fact, you should do this as a standard practice before you work with stand-alone XML documents in Flash.

Flash contains a nonvalidating parser. This means that Flash wont check whether your XML document has a valid structure. If you are using your XML document with other applications, it will be beneficial to check the validity of the XML document before you load it into Flash. You can use an XML editor such as XMLSpy to check an XML document against a DTD or schema.

Use the right tool for your dynamic content

XML is a hot topic in the web development world, and it can be tempting to assume that you should use XML for every Flash project requiring dynamic data. While the XML class makes working with XML documents in Flash easy, there may be some cases where a LoadVars class is a better solution.

XML documents are verbose by definition and have the potential to create large file sizes. If youre querying a database, it may be quicker to send the content into Flash using a variable pair string and the LoadVars class. Youll certainly be able to reduce the size of incoming information when compared with an XML document.

The XML class doesnt create a persistent connection to the web server. If you use the XML class in this situation, youll have to poll the web server continually looking for changes. A better alternative in this case is to use the XMLSocket class. Well look at this class in more detail in Chapter 10 of the book.



Foundation XML for Flash
Foundation XML for Flash
ISBN: 1590595432
EAN: 2147483647
Year: 2003
Pages: 93
Authors: Sas Jacobs

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net