Tuesday, July 28, 2009

Refining the trace statements to get selected tags from XML

In this tutorial we will try and get going with the xml file that we have just loaded in to our flash environment and do some combinations to know how things go.

You can always refer the first part of this series to know how we loaded the xml file and traced out the xml content to the output window. Now we are getting the raw data in the output window in xml format. Now we will try to get a few specific tags being listed and also try out some conditions.

The first part code is here:

var xmlLoader:URLLoader = new URLLoader();

var urlRequest:URLRequest = new URLRequest("schoolXML.xml");
xmlLoader.addEventListener(Event.COMPLETE, xmlLoadedFunc);


var schoolXML:XML = new XML();
schoolXML.ignoreWhitespace = true;

function xmlLoadedFunc (evt:Event):void{

schoolXML = XML(xmlLoader.data);
trace (schoolXML);


Now we will play around with the previous code.

function xmlLoadedFunc (evt:Event):void{
schoolXML = XML(xmlLoader.data);
trace (schoolXML.student.name);

Here, I have just changed the trace statement a little. I have added ".student.name” which now tells the compiler that we are not looking for the complete data but just the data in name which is under student tag which is actually inside the xml file. So if we do this, we can see the list of students. But even now we can see the data in xml tags.

If we like to look at any specific item number then even we can do that. We just have to remember that in the variable all the xml data is stored in the form of array. And so if we have to call any particular data from xml then we have to use the syntax that we use to call data in arrays. And always the numbering starts from 0. So let’s say I want to see the details of the student in the 4th tag. To do this, I have to understand that the record should be stored in array number 3. So I will modify the last statement to something like this

function xmlLoadedFunc (evt:Event):void{
schoolXML = XML(xmlLoader.data);
trace (schoolXML.student.name[3]);

And we can get the desired output.

Now let’s get into more realistic situations. Like want to see how many students scored marks less than 40. May be they will need extra care and so I want to list out the names. So for this, we have to use conditions to get the desired output. Well it may sound a bit difficult, but let me tell you its very easy. As if you are talking to the computer and it will understand the same instruction.

trace (schoolXML.student.(percentage < 50));

yes, this is the trace statement in the function which will give us the desired output. Like if you are using the same xml that I am using then you will get a list of only two students who scored less than 50.

In this trace statement the instruction is very simple, we are asking to trace the records in schoolXML.student and then in the percentage we are giving the condition and that is why we have it inside the brackets.

No comments:

Post a Comment