Monday 23 January 2012

Traversing XML structures - Loops

I am working on a AS3 project with an XML data sheet and this week I figured out another solution to traversing the structure in a better fashion...

<?xml version="1.0" encoding="utf-8"?>
<TIMELINE>
 <YEAR NAME="2000">
  <ARTICLE>
   <MONTH>January</MONTH>
   <NAME>Lorem Ipsum</NAME>
   <DESCRIPTION>Lorem Ipsum is simply dummy text.</DESCRIPTION>
   <IMAGE>image1.jpg</IMAGE>
  </ARTICLE>
  <ARTICLE>
   <MONTH>December</MONTH>
   <NAME>Ipsum Lorem</NAME>
   <DESCRIPTION>Lorem Ipsum is simply dummy text.</DESCRIPTION>
   <IMAGE>image2.jpg</IMAGE>
  </ARTICLE>  
 </YEAR>
 <YEAR NAME="2003">
  <ARTICLE>
   <MONTH>April</MONTH>
   <NAME>Ipsum Lorem Ipsum</NAME>
   <DESCRIPTION>Lorem Ipsum is simply dummy text.</DESCRIPTION>
   <IMAGE>image3.jpg</IMAGE>
  </ARTICLE>  
 </YEAR>
</TIMELINE>

Basically, in my XML sheet, there were years, and within those years there could be more than one article. Some years had numerous articles. Some years had only one article.

myXML.*.*.length();

When I traced something like this, I would get the total number of articles. I could also traverse all the articles as if the year was not of consequence and all the articles were just being counted and assigned numbers in order from 0 onwards.

myXML.*.ARTICLE[articleNum].DESCRIPTION.toString();

The quandrary was that I needed to be able to scroll through all the articles chronologically but I needed to be able to tell what year it was from, according to the article number (which was now effectively independent from the year), and also be able to find out the article number within that year alone, rather than as a whole.

The solution I came up with: Use a loop to check for number of articles in all previous years, add them up so i can subtract it from the full number of articles. This gives you the article number within that year alone, instead of the article number within the entire "timeline" sheet.

var g:Number = 0;
//myXML.*.ARTICLE[articleNum].parent().childIndex() is year index for that article
while (g < myXML.*.ARTICLE[articleNum].parent().childIndex())
   {
        //myXML.YEAR[g].*.length() is the number of articles in g year
        articleNumContainer += myXML.YEAR[g].*.length();
        g += 1;
   }
// one is added to articleNum cos it actually starts at zero
articleNumberWithinYear = (articleNum + 1) - articleNumContainer;



Flash AS3 and SoThink SWF Decompiler

While saving my work, I had an error and lost the FLA while fortunately, having first published a perfect SWF. If you are saving your work in Flash and Flash crashes before it saves, it often causes your FLA to completely disappear. Always backup your work periodically. Thus I was forced to decompile my own code with SoThink SWF Decompiler to recover my work into an FLA file (for speed) but I also realised that it had decompiled it in a different way from how I had originally coded it. It was easy to figure out and I recovered my entire file within an hour to almost the exact way I had originally coded it. Here are some notes on what I noticed about how it had changed my code in the process of decompiling from swf to fla.
  1. All variables that are randomly declared all over the code are collected and put at the top of the code.
  2. New variables are given names like "_loc_2" or "_loc_3". Anything with many brackets is also assigned various variable names for different parts of the equation, and then a few of these temporary functions are added up together instead.
  3. All for loops are turned into while loops.
  4. "this." is added to any objects referred to on the main timeline
  5. It also makes use of the rather uncommon addFrameScript, which adds script to movieclips in AS3. If you leave bits of code outside functions by mistake (eg stage display items), it might group them together in a function called frame1.
  6. It adds the "end function" comment at the end of functions.
Example of typical while loop as decompiled in SoThink:
while (t < max_T_Number)
            {
                // do this
                t = (t + 1);
            }
Example of typical addFrameScript function as decompiled in SoThink:
public function MainTimeline()
        {
            addFrameScript(0, this.frame1);
            return;
        }// end function

        function frame1()
        {
            stage.scaleMode = StageScaleMode.SHOW_ALL;
            stage.align = StageAlign.TOP_LEFT;
            stage.displayState = StageDisplayState.FULL_SCREEN;
        }// end function
}

Although the decompiler is perhaps not the most ethical way of accessing other people's source code, I think the decompiler still makes an excellent learning tool for flash. When I have more time I will download more swfs or analyse more of my old swfs to see if the conditions listed above are consistently true.

No comments:

Post a Comment