XML File Format

XML is designed to be "eXentsible" i.e. you can form it to look any way you want. Like HTML, you use tags and attributes to define what items look like.

XML files start with the following line at the top of the file.

<?xml version="1.0" encoding="ISO-8859-1"?> 

All tags opened must be closed, or self closing. Example:

<note>
 	<to>You</to>
 	<from>Me</from>
 	<heading>Reminder</heading>
 	<body>Don't forget the class assignment this week.</body>
</note> 

Example of longer XML file.

No special characters, i.e. <, >, &. They all have to be encoded. Think HTML strict, but where the application actually forces you to follow the rules.

XML files can get large because of the amount of extra tags they require, so many have gone to JSON, because they are smaller, and easier to work with. However, there is a lot of existing code, and applications that work with XML, so you have to be familiar with it.

The basics of loading an external XML file, are like loading any other file we've deal with previously. You will need to establish a loader object, and listen for when the file is completed. The difference, is you will need to store the XML data into an XML data type.

var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();
xmlLoader.addEventListener(Event.COMPLETE, LoadXML);

xmlLoader.load(new URLRequest("sampleXML.xml"));
function LoadXML(e:Event):void { 
	xmlData = new XML(e.target.data);
	trace(xmlData);
}

From here we will need to write a parser to figure out what data is stored in the XML file.

Parsing has gotten much easier in AS3 luckily. Now you can simply refer to node names:

toData.text = xmlData.to;
fromData.text = xmlData.from;