6 Replies Latest reply on Nov 26, 2009 2:41 AM by sagisid

    Using HTTPService to import data from a XML file

    Finalversion Level 1
      Hello there!

      I'm having some problem's with this import... If anyone can help, I would appreciate it!

      I'm using this type of information as data source:

      public var dataCollection:ArrayCollection =
      new ArrayCollection([
      { id: "P1", name: "Porto", type: "team", children: [
      { id: "R1", name: "Dr Silva", location: "Bloco 1", type: "member" },
      { id: "R2", name: "Dra Neto", location: "Gabinete", type: "member"
      }]
      },
      { id: "P2", name: "Braga", type: "team", children: [
      { id: "R3", name: "Dr Santos", location: "Bloco 2", type: "member" },
      { id: "R4", name: "Dra Sonia", location: "Piso 1", type: "member"
      }]
      }
      ]);

      But I want to import it from a XML file like this:

      <?xml version="1.0" encoding="utf-8"?>
      <items>
      <item id="P1" name="Porto" type="team">
      <children id="R1" name="Dr Silva" location="Bloco 1" type="member" />
      <children id="R2" name="Dra Neto" location="Gabinete" type="member" />
      </item>
      <item id="P2" name="Braga" type="team">
      <children id="R3" name="Dr Santos" location="Bloco 2" type="member" />
      <children id="R4" name="Dra Sonia" location="Piso 1" type="member" />
      </item>
      </items>

      I already import the file, but can not translate the data into a array collection.

      private function initApp():void {
      var httpService:HTTPService = new HTTPService();
      httpService.url = "dataprovider.xml";
      httpService.resultFormat = "e4x";
      httpService.addEventListener(FaultEvent.FAULT, onFaultHttpService);
      httpService.addEventListener(ResultEvent.RESULT, onResultHttpService);
      httpService.send();
      }

      private function onFaultHttpService(e:FaultEvent):void
      {
      Alert.show("Error reading data file.");
      }

      private function onResultHttpService(e:ResultEvent):void
      {
      //Convert the xml data to a array collection
      }

      Thank you
        • 1. Re: Using HTTPService to import data from a XML file
          Peter Hahmann Level 1
          You'll need to loop your xml, in order to get each node(object) and add it to your Collection. If you need an example I could get you one.

          Regards,
          Peter
          • 2. Re: Using HTTPService to import data from a XML file
            Level 7

            "Finalversion" <webforumsuser@macromedia.com> wrote in message
            news:gr2qoj$pq7$1@forums.macromedia.com...
            > Hello there!
            >
            > I'm having some problem's with this import... If anyone can help, I would
            > appreciate it!
            >
            > I'm using this type of information as data source:
            >
            > public var dataCollection:ArrayCollection =
            > new ArrayCollection([
            > { id: "P1", name: "Porto", type: "team", children: [
            > { id: "R1", name: "Dr Silva", location: "Bloco 1", type: "member" },
            > { id: "R2", name: "Dra Neto", location: "Gabinete", type: "member"
            > }]
            > },
            > { id: "P2", name: "Braga", type: "team", children: [
            > { id: "R3", name: "Dr Santos", location: "Bloco 2", type: "member" },
            > { id: "R4", name: "Dra Sonia", location: "Piso 1", type: "member"
            > }]
            > }
            > ]);
            >
            > But I want to import it from a XML file like this:
            >
            > <?xml version="1.0" encoding="utf-8"?>
            > <items>
            > <item id="P1" name="Porto" type="team">
            > <children id="R1" name="Dr Silva" location="Bloco 1" type="member"
            > />
            > <children id="R2" name="Dra Neto" location="Gabinete"
            > type="member" />
            > </item>
            > <item id="P2" name="Braga" type="team">
            > <children id="R3" name="Dr Santos" location="Bloco 2"
            > type="member" />
            > <children id="R4" name="Dra Sonia" location="Piso 1" type="member"
            > />
            > </item>
            > </items>
            >
            > I already import the file, but can not translate the data into a array
            > collection.

            Why not just use it as an XMLListCollection?


            • 3. Re: Using HTTPService to import data from a XML file
              Finalversion Level 1
              quote:

              If you need an example I could get you one.

              I would like that if possible.

              quote:

              Why not just use it as an XMLListCollection?

              Because the whole package is using that kind of data handling, I wouldn't risk to change that. :S

              Thank you for your reply.
              • 4. Re: Using HTTPService to import data from a XML file
                Peter Hahmann Level 1
                Guess this is what you need.

                Regards,
                peter

                • 5. Re: Using HTTPService to import data from a XML file
                  Finalversion Level 1
                  Hello Peter, and thank you for your reply's.

                  My problem is that I'm receiving the data from the external file and I don't know how to get the children in place... I mean, I also have some data being received form a file that I can convert into an array collection, but the thing is, that file doesn't have children structure...

                  It's something like this:

                  <?xml version="1.0" encoding="utf-8"?>
                  <items>
                  <item id="T1" resourceId="R1" name="Cardiologia" startTime="25-3-09 8:0:0" endTime="25-3-09 8:30:0" />
                  <item id="T2" resourceId="R2" name="Raio-X" startTime="25-3-09 9:0:0" endTime="25-3-09 9:15:0" />
                  <item id="T3" resourceId="R3" name="Analises" startTime="25-3-09 12:0:0" endTime="25-3-09 12:45:0" />
                  <item id="T4" resourceId="R4" name="Consulta" startTime="26-3-09 8:0:0" endTime="26-3-09 9:0:0" />
                  </items>

                  And I solve it with this (don't know if is the best):

                  private function onResultHttpServiceTask(e:ResultEvent):void
                  {
                  var a:Array = xmlListToObjectArray(e.result.item);
                  tasks = new ArrayCollection(a);
                  }

                  protected function xmlListToObjectArray(xmlList:XMLList):Array
                  {
                  var a:Array = new Array();
                  for each(var xml:XML in xmlList)
                  {
                  var attributes:XMLList = xml.attributes();
                  var o:Object = new Object();
                  for each (var attribute:XML in attributes)
                  {
                  var nodeName:String = attribute.name().toString();
                  var value:*;

                  value = attribute.toString();
                  o[nodeName] = value;
                  }
                  a.push(new ObjectProxy(o));
                  }
                  return a;
                  }

                  But when the children "enter in action" I don't know how to bring them to the array...

                  This code you send it's preaty much the thing I need, but the thing is that I don't know how to call the children with data as e:ResultEvent...

                  If you can help a bit more, I would really appreciate...

                  Thank You
                  • 6. Re: Using HTTPService to import data from a XML file
                    sagisid Level 1

                    i hope this thing works for you....

                     

                    arrCompleteData = new Array;

                    private function fillHandler(e:ResultEvent):void
                      {
                       ListOfItems = e.result as XML;
                      }

                     

                    for each (node in ListOfItems.item)
                       {// will have all the data stored at first time
                        arrCompleteData.push(node);
                       }

                     

                    itemGrid.dataProvider=arrCompleteData;