2 Replies Latest reply on Feb 26, 2008 5:12 AM by m_hartnett

    Need help parsing local XML data

    nIvelyx
      Hi all,

      I'm learning my way around Flex and I have a question for any expert on here, and it is as follows:

      1. I have an XML file that looks like this:

      <?xml version="1.0" encoding="utf-8"?>
      <catset>
      <cat>
      <name>Whiskers</name>
      <attitude>Frisky</attitude>
      </cat>
      </catset>

      2. I went into Flex and created a brand new project. I placed in the following code:

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init();" width="712" height="444">
      <mx:HTTPService id="xmlService" url="xmlData.xml" result="resultHandler(event)" />
      <mx:Script>
      <![CDATA[
      import mx.collections.ArrayCollection;
      import mx.rpc.events.ResultEvent;


      [Bindable]
      private var xdata:ArrayCollection;


      public function init():void {
      xmlService.send();
      }

      private function resultHandler(event:ResultEvent):void {
      xdata = event.result.catset.cat as ArrayCollection;
      }
      ]]>
      </mx:Script>

      <mx:DataGrid dataProvider="{xdata}" width="50%" height="50%">
      <mx:columns>
      <mx:DataGridColumn dataField="name" headerText="Name" />
      <mx:DataGridColumn dataField="attitude" headerText="Attitude" />
      </mx:columns>
      </mx:DataGrid>
      </mx:Application>

      Pretty simple right? When I load the file I don't get any errors, but nothing loads into my XML grid. I've tried Googling, searching forum topics, etc. but all of the tutorials are either super old or simply no longer work. If anyone could take a look at the code above and tell me what I'm doing wrong, I would appreciate it.

      Cheers
        • 1. Need help parsing local XML data
          SujitG Level 2
          Hi,
          Add a fault event handler to your http service, this will help you in checking out what is causing the problem. Can you also check out where the XML file is located. As per the URL you have given the XML file has to be in the folder, where the swf is residing. If you are running your application from the Flex Builder, then the XML file has to be in your bin-debug/bin-release/bin folder.
          HTTPService allows you to set the output format, using the resultFormat property. Check out the URL below for more details
          http://livedocs.adobe.com/labs/flex3/langref/mx/rpc/http/HTTPService.html#resultFormat
          Hope this helps.
          • 2. Need help parsing local XML data
            m_hartnett Level 3
            Here is a working version.

            The problem you are having is a common issue.
            Read this earlier post of mine.
            http://www.adobe.com/cfusion/webforums/forum/messageview.cfm?forumid=60&catid=585&threadid =1319898&highlight_key=y&keyword1=result

            The only other change I made was that the ArrayCollection is initialized to and empty object. If you did not initialize to empty object then you would need to do it in the if statement of the result handler before you add the item.

            private var xdata:ArrayCollection = new ArrayCollection();

            Also SujitG is correct, you should always code a fault handler and throw an Alert box up to show any errors.
            One other tip is that the coding in the resultHandler that checks for Object or ArrayCollection can be put into some kind of utility object that makes the check. This way you dont have to code for the condition in every handler just call the utility. You can always code that later.

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute"
            creationComplete="init();" width="712" height="444">

            <mx:HTTPService id="xmlService" url="xmlData.xml" result="resultHandler(event)"/>

            <mx:Script>
            <![CDATA[
            import mx.collections.ArrayCollection;
            import mx.rpc.events.ResultEvent;


            [Bindable]
            private var xdata:ArrayCollection = new ArrayCollection();


            public function init():void {
            xmlService.send();
            }

            private function faultHandler(event:ResultEvent):void {
            xdata = event.result.catset.cat as ArrayCollection;
            }

            private function resultHandler(event:ResultEvent):void {
            if(event.result.catset.cat is ArrayCollection)
            xdata = event.result.catset.cat as ArrayCollection;
            else
            xdata.addItem( event.result.catset.cat);
            }
            ]]>
            </mx:Script>

            <mx:DataGrid dataProvider="{xdata}" width="50%" height="50%">
            <mx:columns>
            <mx:DataGridColumn dataField="name" headerText="Name" />
            <mx:DataGridColumn dataField="attitude" headerText="Attitude" />
            </mx:columns>
            </mx:DataGrid>
            </mx:Application>