| Ultrashock Tutorials > Flash5 >Introduction to XML | ||||
|
||||
| Introduction to XML | ||||
|
Why Bother? XML is quickly spreading throughout the web world and it's among the new technologies supported by Flash5. XML is new territory for many Flash developers and before you can really exploit it you need to get a handle on what it is. Let's say first off that XML is no magical solution - it simple provides a more elegant way to pass around data. The Flash XML object includes methods to create, manipulate and transfer information using this standard format. If you've been working with Flash you'll probably be familiar with the fact that we can pass name/value pairs in and out of Flash using various strategies. For example, we can pass data into Flash using a query string like this: myflashfile.swf?players=2&highscore=564 But as soon as our information gets more complex things get messy.
file.swf?item01=234&name01=buddy&age01=15&item02=768 List structures like this are unwieldy when our goal is to pass the data with some of its context intact. Enter XML.
What is it? XML (or Extensible Markup Language) is a standard for a customizable markup language. It has been approved as a standard by the World Wide Web Consortium and adopted by dozens of companies including Microsoft, Netscape and Macromedia. It also forms the basis of formats such as SMIL (for streaming media) and SVG (scalable vector graphics). XML, like other markup languages, allows us to combine data with descriptive tags which help us make sense of that data. Unlike a predefined markup language like HTML, XML isn't designed with one specific use in mind. In fact, XML let's us define the tags and attributes. This makes XML flexible enough to handle a variety of quite complex data structures - everything from your shopping list to your stock portfolio.
Let's take a look Each XML document can be designed to suit the data it carries but all share a few basic features. All XML documents are made up of elements which include a chunk of data and the enclosing tags. The tags tell us something about the information they contain and can include one or more attributes.
Let's take a look at a simple example of an XML document - a list of movies.
<?xml version="1.0"?> XML documents always start with a prolog which lets everyone know we're talking XML. Next comes our root tag - in this case it's a MOVIELIST tag. There is only ever one root tag in an XML document. Next are a pair of MOVIE elements which include three nested tags. An element is a chunk of data contained within a pair of tags. They may or may not include nested tags. So we could refer to the YEAR element or the MOVIE element. Notice that the MOVIE tag includes an attribute - the ID number. An attribute is a name/value pair that is added within a tag. Attributes are best used to provide descriptive information to tell us more about the element. Here's a view of our movie list using many more attributes.
<?xml version="1.0"?> Bad idea. Although this sort of 'shorthand' can be tempting, you are better off putting your data within tags and not grouped within attributes. For one thing, attributes can't handle multiple values (what if there were two titles for a film?).
Are you well-formed? A "well-formed" XML document is one which fits the basic rules defined by the W3C standard. These rules are more strict than you might be used to with HTML. Here are a few key rules: All tags need to be balanced: All XML tags must have corresponding closing tag. There is a special case for stand-alone tags - they must include a trailing slash such as: <MSG TEXT="Hi there" />. Tags are case-senstive: <tag> <TAG> and <Tag> are all considered different. So something like <Name>Jason</NAME> breaks the rules. All attribute values need to be in quotes: HTML is pretty forgiving of this, XML is not. You can use either single or double quotes. If your attribute value includes quotes then use something like <MSG TXT='Joe says "hi"'>. Elements must be properly nested: No crossing your tags. <B><I>Text</B></I> just won't work.
What about the DTD? A Document Type Definition (DTD) is a document which sets the rules for your particular breed of XML document. There are also standard DTDs for some established needs such as SMIL for streaming media and SVG for vector graphics. The DTD gives detailed information about what type of data to expect in each tag. They are often used to test XML documents to make sure they conform to an expected format before an application attempts to use them. Flash does not worry about a verifying XML documents with a DTD. This means that you have to trust that the XML you are working with is in the format you expect it to be. It also means that you may have to live with the consequences if you feed your program some unexpected format.
The XML Object Flash developers will be using the built-in XML object to read and generate XML documents. Flash can parse an XML document, creating an object which contains all our data. We play with it in object form and when we're done we can use one of the built-in methods to generate a document and send it to a server. The ins and outs of how this works in actionscript is the fodder for another article.
Wrap up Pretty sexy stuff eh? Maybe not, but it's powerful. To get some idea of the possibilities with XML just look at the long list of companies, applications, databases, servers and content providers who deal in XML every day.
|
||||
|
©2001 Ultrashock.com Inc.
- All rights reserved
|