2 Replies Latest reply on Sep 25, 2009 12:24 PM by cksachdev

    Stepping through a poorly formatted xml file

    Zaph31-spayNX

      Hi, this forum worked a charm earlier so I'm hoping it will go 2 for 2.

       

      Through a httpservice I'm reading an XML file into a ArrayCollection. Because the XML is so poorly formatted I'm having to write some very sloppy code just to step through 20 results. I have no control over the XML.

       

      XML Example (can be anywhere from 0 to 20 results):

       

      <results>
      
          <game>
              <type>game</type>
              <id>321</id>
              <name>gamename</name>
              <image>i34d.png</image>
          </game>
      
      
          <character>
              <type>character</type>
              <id>1123</id>
              <name>john</name>
              <image>sds.png/image>
          </character>
      
          <game>
              <type>game</type>
              <id>432</id>
              <name>examplename</name>
              <image>g2dss.png</image>
          </game>
      
      </results>
      

       

      Each result can be one of 7 types (I've done only 2, game and character, in my example). The order of them I really want to preserve in my ArrayCollection as it's very relevant.

       

      Here is the (very) sloppy way I'm currently loading each result into my AC:

      //container array
      var ac2:ArrayCollection = new ArrayCollection;
      
      //I do a block like this for EVERY results type. Meaning 7 in total...
      if (service2.lastResult.response.results.character) {
           var characterAC:ArrayCollection = new ArrayCollection;
           if (service2.lastResult.response.results.character is ArrayCollection) {
           //there is more than 1 entry
           characterAC = service2.lastResult.response.results.character as ArrayCollection;
           } else {
                //There is only 1 entry. Need to MAKE it an ArrayColleciton
                characterAC = new ArrayCollection([service2.lastResult.response.results.character]);
           }
              //Add all characters to AC
           ac2.addAll(characterAC);
           }
      

       

      The two major problems with this code is: 1, it's stupidly bloated and 2, ac2 no longer maintains the order of the XML response. First it puts all <character> into my AC, then it puts all <game> into AC. So on and so forth.

       

      In my head I'm trying to do something like this:

      ac2 = service2.lastResult.response.results.* as ArrayCollection;

      That would then load all results (regardless of type) into an ArrayCollection. I'm guessing a solution is not that simple...

       

      In an Ideal world, whoever created the XML api would just call each result <result> instead of the stupid type tags it currently uses.

       

      I hope all that makes sense.

       

      Regards,

      Zaph.

        • 1. Re: Stepping through a poorly formatted xml file
          Peter deHaan Level 4

          @Zaph31,

           

          Why not use an XMLListCollection instead of an ArrayCollection?

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" initialize="init();">
          
              <mx:XML id="iWouldntHaveCodedXMLLikeThat" xmlns="">
                  <results>
                      <game>
                          <type>game</type>
                          <id>321</id>
                          <name>gamename</name>
                          <image>i34d.png</image>
                      </game>
                      <character>
                          <type>character</type>
                          <id>1123</id>
                          <name>john</name>
                          <image>sds.png</image>
                      </character>
                      <game>
                          <type>game</type>
                          <id>432</id>
                          <name>examplename</name>
                          <image>g2dss.png</image>
                      </game>
                  </results>
              </mx:XML>
          
              <mx:XMLListCollection id="xmlListColl" />
          
              <mx:Script>
                  <![CDATA[
                      private function init():void {
                          xmlListColl.source = iWouldntHaveCodedXMLLikeThat.children();
                      }
                  ]]>
              </mx:Script>
          
              <mx:DataGrid id="dg" dataProvider="{xmlListColl}">
                  <mx:columns>
                      <mx:DataGridColumn dataField="type" />
                      <mx:DataGridColumn dataField="id" />
                      <mx:DataGridColumn dataField="name" />
                      <mx:DataGridColumn dataField="image" />
                  </mx:columns>
              </mx:DataGrid>
          
          </mx:Application>
           
          

           

           

          Peter

          • 2. Re: Stepping through a poorly formatted xml file
            cksachdev Level 1

            @Zaph31:

            XMLListCollection is all you need.

             

            --

            Chetan Sachdev

            image: Linkedin <http://www.linkedin.com/in/cksachdev>[image:

            Twitter]<http://twitter.com/cksachdev