Search code examples
xmlapache-flexadobeloadingmxml

Loading a xml file into mxml(Adobe Flex)


I have actually found this code on stackoverflow itself. I want to load an xml into mxml and then convert the loaded data into a pie chart or a bar graph. The code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/mx" 
                       creationComplete="load()">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->         
    </fx:Declarations>
    <fx:Script>
        <![CDATA[

            // this is a variable definition so it is good
            var xmlLoader:URLLoader = new URLLoader();

            // these two lines are code that executes; so they must be put inside a method; something you did not do.  Comment them out 
            //xmlLoader.addEventListener(Event.COMPLETE, loadXML); // 1st error here
            //xmlLoader.load(new URLRequest("books.xml")); // 2nd error here

            // this is a variable definition so it is okay
            var xmlData:XML = new XML();

            // this is a function definition so it is okay
            function loadXML(e:Event):void{             
                xmlData = new XML (e.target.data);              
            }               

            // move your executing code into a method
            public function load():void{
                xmlLoader.addEventListener(Event.COMPLETE, loadXML);
                xmlLoader.load(new URLRequest("books.xml"));
            }
        ]]>
    </fx:Script>

    <s:VGroup>      
        <s:TextArea id="txtArea">       
        </s:TextArea>               
    </s:VGroup>

</s:WindowedApplication>

It's a code I found on stackoverflow. But still I'm not able to run the code.

And the xml file is something like this:

<?xml version="1.0" encoding="utf-8"?>
<Books>
<Manager Man_id="1" Man_Name="P.Ananth"/>
<Manager Man_id="2" Man_Name="Gokulakrishnan"/>
<Manager Man_id="3" Man_Name="Stani"/>
</Books>

The error I'm incurring is this:- Unable to locate specified base class 'spark.components.WindowedApplication' for component class 'loader2'. loader2.mxml -- /loader2/src -- Unknown Flex Problem


Solution

  • It seems that your project is Flex web project.

    The WindowedApplication is included in the AIR SDK, meaning your project needs to be desktop application project.

    Try,

    • <s:WindowedApplication> to <s:Application>

    Added:

    Although the URLLoader is very important class, you may want to use HTTPService in general.

    Let's take a look at code that does the same as your code above:

    <?xml version="1.0" encoding="utf-8"?>
    <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                   xmlns:s="library://ns.adobe.com/flex/spark"
                   xmlns:mx="library://ns.adobe.com/flex/mx"
                   creationComplete="service.send()">
    
        <fx:Declarations>
            <s:HTTPService id="service" resultFormat="e4x" url="books.xml"/>        
        </fx:Declarations>
    
        <s:VGroup>
            <s:TextArea id="txtArea" width="100%" text="{service.lastResult}"/>       
        </s:VGroup>
    
    </s:Application>
    

    Isn't it simple?

    URLLoader is as3 class, and the Flex framework is built on as3. HTTPService is the Flex component, which provides you easy and convenient features. It uses URLLoader internally.