# Help on AS3/XML converting this code into MVC design

**URL:** <https://forum.kirupa.com/t/help-on-as3-xml-converting-this-code-into-mvc-design/327500>\
**Category:** Uncategorized\
**Created:** [March 5, 2012, 8:21pm UTC](https://forum.kirupa.com/t/help-on-as3-xml-converting-this-code-into-mvc-design/327500 "2012-03-05T20:21:41Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![theNub](https://avatars.discourse-cdn.com/v4/letter/t/74df32/32.png) [@theNub](https://forum.kirupa.com/u/theNub)\
**Post date:** [March 5, 2012, 8:21pm UTC](https://forum.kirupa.com/t/help-on-as3-xml-converting-this-code-into-mvc-design/327500/1 "2012-03-05T20:21:41Z")

</div>

Hi, I’m still pretty new to OOP design, as3, & XML. I’ve been trying to re-create this code into MVC design pattern and have not successfully done it. I’ve been going at for a few hours and no luck. This code is a dynamic diagram which users loads the info from the xml data onto the as3. If anyone could please help me with this, I’d so appreciate it. Thanks.

-theNub:shifty:

\<?xml version=“1.0”?\>  
\<graphs width=“50”\>  
\<graph name=“Yellow” value=“50” color=“0xFDC13E”/\>  
\<graph name=“Blue” value=“80” color=“0x25B7E2”/\>  
\<graph name=“Green” value=“30” color=“0xB8CF36”/\>  
\<graph name=“Red” value=“10” color=“0xE7473F”/\>  
\</graphs\>

package  
{  
import fl.transitions.Tween;  
import fl.transitions.easing.Strong;  
import flash.display.Sprite;  
import flash.text.TextField;  
import flash.text.TextFormat;  
import flash.text.TextFieldAutoSize;  
import flash.net.URLLoader;  
import flash.net.URLRequest;  
import flash.events.Event;

```
public class Graph extends Sprite
{

 private var graphContainer:Sprite = new Sprite();
         private var xmlFile:XML;
         private var urlLoader:URLLoader = new URLLoader();
         private var totalBars:int;

         private var tween:Tween;
         private var tf:TextFormat = new TextFormat();

    public function Graph():void
    {
          /* Text Format */

              tf.color = 0x666666;
              tf.size = 12;
              tf.font = "Helvetica";

              createGraphContainer();
              loadXML();
              createBars();
    }

    private function createGraphContainer():void
    {
               graphContainer.graphics.lineStyle(1, 0x9C9C9E);
               graphContainer.graphics.moveTo(30, 30);
               graphContainer.graphics.lineTo(30, 270);
               graphContainer.graphics.lineTo(570, 270);

               addChild(graphContainer);
    }

    private function loadXML(file:String = "graph.xml"):void
    {
              urlLoader.load(new URLRequest(file));
              urlLoader.addEventListener(Event.COMPLETE, parseXML);
    }

    private function parseXML(e:Event):void
    {
              xmlFile = new XML(e.target.data);
              totalBars = xmlFile.children().length();

              createBars();
              displayNames();
    }

    private function createBars():void
    {
              for (var i:int = 0; i &lt; totalBars; i++)
        {
                 var bar:Sprite = new Sprite();

                 bar.graphics.beginFill(xmlFile.children()*.@color);
                 bar.graphics.drawRect(0, 0, xmlFile.@width, xmlFile.children()*.@value);
                 bar.graphics.endFill();

                 bar.x = 40 + (xmlFile.@width * i) + (10*i);
                 bar.y = 270 - bar.height;

                 var val:TextField = new TextField();

                 val.defaultTextFormat = tf;
                 val.autoSize = TextFieldAutoSize.RIGHT;
                 val.text = xmlFile.children()*. @ value;

                 val.x = 55 + (xmlFile.@width * i) + (10*i);
                 val.y = 255 - bar.height;

                 tween = new Tween(bar,"height",Strong.easeOut,0,bar.height,1,true);
            
                 addChild(bar);
                 addChild(val);
          }
    }

    private function displayNames():void
    {
             for (var i:int = 0; i &lt; totalBars; i++)
        {
                 var color:Sprite = new Sprite();
                 var names:TextField = new TextField();

                 names.defaultTextFormat = tf;
                 names.autoSize = TextFieldAutoSize.LEFT;
                 names.text = xmlFile.children()*. @ name;

                 names.x = 500;
                 names.y = 30 + (20 * i);

                 color.graphics.beginFill(xmlFile.children()*.@color);
                 color.graphics.drawRect(0, 0, 10, 10);
                 color.graphics.endFill();

                 color.x = 490;
                 color.y = 31 + (20 * i);

                 addChild(names);
                 addChild(color);
        }
    }
}

```

}
