9 Replies Latest reply on Dec 21, 2009 10:27 AM by Francisc

    How to read xml data?

    Charu Shukla

      I need urgent help with this code to read xml nodes from an xml file.

       

       

      This is the HttpService

       

      <mx:HTTPService id="appraisalData"
                          url="assets/appraisal.xml"
                          result="HandleDataResult(event)"
                           />

       

      ::SCRIPT::

       

      [Bindable]
                  private var _appData:XMLList=new XMLList();

       

                  private function HandleDataResult(event:ResultEvent):void
                  {
                                  _appData=event.result.appraisal.APPRAISAL;
                     
                     
                  }

       

       

      I want to extract nodes from this XMLList and fill my components

       

      Like:

      <components:HeaderSection width="100%"  headerData="{XML(_appData[0])}"/>

       

       

      Here's the XML file sample:

       

      <?xml version="1.0"?>
      <APPRAISAL>
      <ACTIONS/>
      <RATINGSTATUS>
      <MIDYEAR>
      <EDITABLE>false</EDITABLE>
      <VISIBLE>true</VISIBLE>
      </MIDYEAR><YEAREND>
      <EDITABLE>false</EDITABLE>
      <VISIBLE>true</VISIBLE>
      </YEAREND></RATINGSTATUS>
      <HEADER>
      <PLAN_VERSION>01</PLAN_VERSION>
      <APPRAISAL_ID>4B1DF5B7B24100A7E1008000AC11A294</APPRAISAL_ID>
      <EMPLOYEE>Tom Smith</EMPLOYEE>
      <LEADER>Bob string hello Jr</LEADER>
      <APPRAISAL_TYPE>2010 Individual Contributor</APPRAISAL_TYPE>
      <AP_START_DATE>01/01/2009</AP_START_DATE>
      <AP_END_DATE>12/31/2009</AP_END_DATE>
      <AP_STATUS>2</AP_STATUS>
      <AP_STATUS_NAME>In Planning</AP_STATUS_NAME>
      <AP_STATUS_SUB>4</AP_STATUS_SUB>
      <AP_STATUS_SUB_NAME>Pending Transfer</AP_STATUS_SUB_NAME>
      <ORGUNIT>Payroll Test Org.2</ORGUNIT>
      <PERSONAL_AREA>CA-Catalina Island</PERSONAL_AREA>
      <PERSONAL_SUBAREA>Teamsters - 495</PERSONAL_SUBAREA>
      <EE_GROUP>Hourly</EE_GROUP>
      <EE_SUBGROUP>Full Time Non-Exempt</EE_SUBGROUP>
      </HEADER>

        • 1. Re: How to read xml data?
          Francisc Level 3

          If you can wait until I get home, I'll write the answer... 8h or so until then.

          Otherwise, use the Debug view to see if you are reading the XML and if so, how it gets there.

          If that won't help, look here: http://buildinternet.com/2008/12/xml-basics-with-flex-3/

          If you follow those steps you will have your XML imported in no time.

           

          Good luck!

          1 person found this helpful
          • 2. Re: How to read xml data?
            Charu Shukla Level 1

            Thanks for the reply, hope you get home soon and help me out.

            • 3. Re: How to read xml data?
              pauland Level 4

              Charu, you'll learn more by trying yourself first. There's loads of articles out there on google that will get you started.

               

              Better to have a go then get help with your attempt than have a solution on a plate.

               

              Paul

              • 4. Re: How to read xml data?
                Charu Shukla Level 1

                Well i have been trying it out for few days now, am not able to find the right solution.

                • 5. Re: How to read xml data?
                  Matt Le Fevre Level 4

                  reading XML is really a bread and butter matter when it comes to Flex,

                   

                  just watch where your Tags begin and End, and you really shouldn't come into any problems.

                   

                  there are loadssssssssssssss of examples out there, just find a working one, look at the source code and you'll be sorted

                  • 6. Re: How to read xml data?
                    vksvicky

                    Charu, try this

                     

                    [Bindable] private var testXML:XML;

                     

                    protected function init():void {

                        var serviceObj:HTTPService = new HTTPService();

                        serviceObj.resultFormat = 'e4x';

                        serviceObj.method = 'GET';

                        serviceObj.useProxy = false;

                        serviceObj.addEventListener(ResultEvent.RESULT, processResult);

                        serviceObj.url = 'wassets/appraisal.xml';

                        serviceObj.send();

                    }

                     

                    // The result processing function

                    protected function processResult(response:ResultEvent):void {

                        // make sure the response is cast as the correct data type

                        testXML = response.result as XML;

                    }

                     

                    Now u can use the testXML in your Datagrid.

                    1 person found this helpful
                    • 7. Re: How to read xml data?
                      pauland Level 4

                      The XML in your example is not correctly formed - start there first.

                      • 8. Re: How to read xml data?
                        Francisc Level 3

                        Honeeeey, I'm hooome! So... here's the code:

                         

                         

                        <mx:Script>

                        <![CDATA[

                         

                        import mx.controls.Alert;

                        import mx.rpc.events.FaultEvent;

                        import mx.rpc.events.ResultEvent;

                        import mx.collections.ArrayCollection;

                         

                        private var myRetrivedData:ArrayCollection;

                         

                        private function getXMLData():void

                        {

                             httpService.send();

                        }

                        private function httpFaultHandler(event:FaultEvent):void

                        {

                             Alert.show("Cannot load XML file.","Error");

                        }

                        private function httpResultHandler(event:ResultEvent):void

                        {

                             myRetrivedData=event.result.elements.element;

                        }

                        ]]>

                        </mx:Script>

                         

                        <mx:HTTPService id="httpService" url="someXMLFile.xml"

                             fault="httpFaultHandler(event)" result="httpResultHandler(event)"/>

                         

                        <mx:DataGrid id="myDataGrid" dataProvider="{myRetrivedData}"/>

                        • 9. Re: How to read xml data?
                          Francisc Level 3

                          Your XML is rather odd as well.

                          You should format it simmilar to this for example:

                           

                          <players>

                               <player>

                                    <name>Wayne Rooney</name>

                                    <position>Striker</position>

                                    <number>10</numer>

                                    <goals>20</goals>

                               </player>   

                               <player>

                                    <name>Ryan Giggs</name>

                                    <position>Midfielder</position>

                                    <number>11</numer>

                                    <goals>10</goals>

                               </player>

                               <player>

                                    <name>Anderson</name>

                                    <position>Midfielder</position>

                                    <number>8</numer>

                                    <goals></goals>

                               </player>

                          </players>