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.
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:
The path refers to the text contained within a grandchild of the root node. The use of the firstChild property and the childNodes 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.
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.childNodes; 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.
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.
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.
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.