XML Integration With Flash

New we are going to integrate XML file with Flash. We will read XML data using a flash and display it. To read XML file we are going to deal with inbuilt function provided by macromedia flash. The event we’re dealing with here is the event of the XML being fully downloaded and introduced into Flash movie. This is the called the onLoad event.

[code lang=”actionscript”]
Syntax:
onLoad = function(success:Boolean) {}
[/code]

If the XML document is received successfully, the success parameter is true. If the document was not received, or if an error occurred in receiving the response from the server, the success parameter is false. The default, implementation of this method is not active. To override the default implementation, you must assign a function that contains custom actions.

So when loading XML into Flash, we have to create that XML instance. The XML instance here and most we will see from now on despite the fact that naming is arbitrary, will be called “my_xml.

[code lang=”actionscript”]
my_xml = new XML();
[/code]

An XML document is represented in Flash by the XML class. We can use the methods and properties of the XML class to load, parse, send, build, and manipulate XML document trees. You must use the constructor new XML() to create an XML object before calling any method of the XML class.

Now we are going to load a XML file. my_xml.load(”test.xml”); will do the same for us. it will Loads an XML document from the specified URL, and replaces the contents of the specified XML object with the downloaded XML data. The URL is relative and is called using HTTP. The load process is asynchronous; it does not finish immediately after the load() method is executed.

When the load() method is executed, the XML object property loaded is set to false. When the XML data finishes downloading, the loaded property is set to true, and the onLoad event handler is invoked. The XML data is not parsed until it is completely downloaded. If the XML object previously contained any XML trees, they are discarded.

Now we have to define a custom function that executes when the onLoad event handler of the XML object is invoked. Before that we will first decide what the XML file content and also make a skeleton to display our xml file. We are going to display contents of test.xml which contain Countries and their Capitals. Structure of test.xml as below…

[code lang=”xml”]





[/code]

Design Flash Skeleton

Now we are going to make a flash skeleton for it…
Create a new Flash document File > Flash Document or by Ctrl+N and modify its property as width ‘250px’ & Height ‘150px’ and background color as black. You can do this through Modify > Document… or by Ctrl+J.

XML Integration with flash skeleton

XML Integration

Now we have to define a function for parse our XML file.

[code lang=”actionscript”]
function parse_xml() {
// take the data from the XML lines (line 0,1,2) and place that data inside text fields
text_field_1.text = my_xml.firstChild.childNodes[0].attributes.country_name;
text_field_2.text = my_xml.firstChild.childNodes[1].attributes.country_name;
text_field_3.text = my_xml.firstChild.childNodes[2].attributes.country_name;
//
text_field_a.text = my_xml.firstChild.childNodes[0].attributes.Capital;
text_field_b.text = my_xml.firstChild.childNodes[1].attributes.Capital;
text_field_c.text = my_xml.firstChild.childNodes[2].attributes.Capital;
}
[/code]

so we get result as… if file now working please click here to see working example





()

Source Code:

Download Source code for ‘ XML Integration with Flash ‘

Download

0 komentar: