Usually, in most programms, the data in the XML format is loaded from external files.
ActionScript 3 contains special functions to load and use the XML content from an external file.
To load data from a XML file, follow these general steps:
<?xml version="1.0"?> <site> <courses> <course id="1" title="PHP-MySQL">coursesweb.net/php-mysql/</course> <course id="2" title="JavaScript">coursesweb.net/javascript/</course> <course id="3" title="FlashActionScript">coursesweb.net/flash/</course> </courses> </site>
// Declare a empty XML instance var test_xml:XML; // Create object with the file's URL var url:URLRequest = new URLRequest('xmltest.xml'); // declare the object that load the file var loader:URLLoader = new URLLoader(url); // register an event listener that detects when the loading has finished, and calls the "getXml()" loader.addEventListener(Event.COMPLETE, getXml); // The Function getXml() called by the event listener function getXml(evt:Event):void { // If data is loaded (stored in the property "data" of the object "loader") if(loader.data) { // Delete the event listener, not being necessary anymore, and it frees up memory loader.removeEventListener(Event.COMPLETE, getXml); // Add the data in the XML instance test_xml = XML(loader.data); /* Accessing the XML loaded data */ var nr_c:int = test_xml.courses.*.length(); // gets the number of elements in <courses> (3) // Returns the name of the root tag trace(test_xml.localName()); // site // gets data in the last element from <courses> (nr_c-1) trace(test_xml.courses.child(nr_c-1)); // coursesweb.net/flash/ } }- Because the 'nr_c' variable stores the number of elements (tags) in <courses>, (nr_c-1) represents the index of the last element, and the expression "test_xml.courses.child(nr_c-1)" returns its content.
Traversing an object means to access every node in that object and process it in some way.
For example, if you want to search through an XML object looking for an element with a certain attribute, the object must be traversed to verify each element.
To traverse every node in an XML tree (or in an XMLList), use the "for each ... in", statement:
for each (var elm:XML in ObjectXML..*) { // code to execute }- ObjectXML..* - returns a list containing all of ObjectXML's descendants.
// Declare a empty XML instance var test_xml:XML; // Create object with the file's URL var url:URLRequest = new URLRequest('xmltest.xml'); // declare the object that load the file var loader:URLLoader = new URLLoader(url); // register an event listener that detects when the loading has finished, and calls the "getXml()" loader.addEventListener(Event.COMPLETE, getXml); // The Function getXml() called by the event listener function getXml(evt:Event):void { // If data is loaded (stored in the property "data" of the object "loader") if(loader.data) { // Delete the event listener loader.removeEventListener(Event.COMPLETE, getXml); // Add the data in the XML instance test_xml = XML(loader.data); // traverse the nodes in every descending element (..*) in "test_xml" for each(var elm:XML in test_xml..*) { // If the current element has the attribute "id", with the value of 2 // outputs the name of that tag and its text content if(elm.hasOwnProperty("@id") && elm.@id==2) trace(elm.localName()+ ' - '+ elm.text()); // Display: course - coursesweb.net/javascript/ } } }- See the explanations in code.
To traverse only the attributes in an XML objecr, use the following formula:
for each(var atr:* in ObiectXML..@*)
- For example, in the script above we can apply:
for each(var atr:* in test_xml..@*) {
if(atr==2) trace(atr.parent().localName()+ ' - '+ atr.parent().text());
}
- "parent()" returns the parent element (the one in which it's included).
- The result displayed is the same.
<object type="application/x-shockwave-flash" data="file.swf" width="500" height="250"> <param name="src" value="file.swf" /> Your browser not support SWF. </object>
input:focus { background-color: #88fe88; }
var jsnstr = '{"url": "http://coursesweb.net/", "title": "Web Development Courses"}'; var obj = JSON.parse(jsnstr); alert(obj.url);
if (file_put_contents("file.txt", "content")) echo "The file was created"; else echo "The file can not be created";