8 Replies Latest reply on Oct 26, 2009 8:06 PM by TarunaR

    How to read XML?

    TarunaR

      I am getting problem in reading this format of XML. I want to go to images > imagesSet.

       

      when i have imgs:Array = this.childNodes[1].childNodes; it brings me both imagesSet. Now i want to pick either of the two. I dont want to define each into an array (like imgs1:Array = this.childNodes[1].childNodes(0).childNodes and like wise imgs2:Array=this.childNodes[1].childNodes(1).childNodes) because in the future there could be more sets of images coming up so dont want to keep adding array for each everytime we add a set.

       

      So can someone help me picking up them from the imageSet.

       

       

       

      XML Code:-

       

      <data>


          <data1 title="Title1" 
          detail="Here goes the text"
          url="xyz">   
          </data1>
          <data2 title="Title2" 
          detail="Here goes the text again"
          url="xyz2">
          </data2>


      </data>

       

      <images>

       

          <imagesSet>
              <image url="images/1.jpg">images/1.jpg</image>
              <image url="images/2.jpg">images/2.jpg</image>
              <image url="images/3.jpg">images/3.jpg</image>   
          </imagesSet>
          <imagesSet>
              <image url="images2/1.jpg">images2/1.jpg</image>
              <image url="images2/2.jpg">images2/1.jpg</image>
              <image url="images2/1.jpg">images2/1.jpg</image>
          </imagesSet>

       

      </images>

       

       

      I did something like this.....

       

      AS Code:-

       

      for (i=0; i<imgs.length; i++) {
              
               imgs_set["name"+j] = imgs[i].childNodes[0].attributes.url; // this is to set the first image as first in the gallery


               for(a=0; a<what will i have here?; a++){
               nextImg[
      "name"+j] = imgs[i].childNodes[a].attributes.url; // this is to pick the other ones from the set

         
               }
           }
           a = 0;

       

      previous.onRelease = function() {
                      
                   if (a>0) {
                       a--;
                       toolTipMC.img.imgHolder.loadMovie(nextImg[(myBtn._name)]); // the
      "name"+j from the array will match with myBtn._name
                   }
               };

      next.onRelease = function() {


               
                   if (a<3) {
                       a++;
                       toolTipMC.img.imgHolder.loadMovie(nextImg[(myBtn._name)]);


                  }
               };

        • 1. Re: How to read XML?
          kglad Adobe Community Professional & MVP

          use the following to retrieve the the first or second array of your image nodes:

           

          yourxml.childNodes[1].firstChild.childNodes or

           

          yourxml.childNodes[1].firstChild.nextSibling.childNodes

          • 2. Re: How to read XML?
            TarunaR Level 1

            Thank you for your reply. I understand what you are saying. The only issue is when we define

             

            myxml.childNodes[1].firstChild.nextSibling.childNodes

             

            we are restricting to the firstChild ie the first SET, i did mention if we have for example 5 sets, i dont want to keep on defining

             

            myxml.childNodes[1].childNodes[0].childNodes

            myxml.childNodes[1].childNodes[1].childNodes

            myxml.childNodes[1].childNodes[2].childNodes

            myxml.childNodes[1].childNodes[3].childNodes

            myxml.childNodes[1].childNodes[4].childNodes

             

            even if i place them in some loop, how to know the length of the childNodes?

            • 3. Re: How to read XML?
              TarunaR Level 1

              Just a further help needed

               

              I modified everything but just need a minor help in figuring out why it is getting an error opening url.

               

              code:-

               

              for (i=1; i<imgs.childNodes.length; i++) {
                      for (a=0; a<imgs.childNodes[i].childNodes.length; a++){
                            
                      nextImg["loc"+i] = imgs.childNodes[i].childNodes[a].attributes.url;
                     
                      }
                  }


                  a = 0;

               

                          toolTipMC.previous.onRelease = function() {
                 
                          if (a>0) {
                              a--;
                              toolTipMC.img.imgHolder.loadMovie(nextImg[myBtn._name]);

               

                          
                           if (a==0) {
                            
                              toolTipMC.img.imgHolder.loadMovie(nextImg[myBtn._name]);
                          }
                      }

                }
                      toolTipMC.next.onRelease = function() {

                          if (a<2) {
                              a++;

                              toolTipMC.img.imgHolder.loadMovie(nextImg[myBtn._name]);

                          }

                      };
              };

               

              XML: changed it completely. took the sets out of the tag images.

               

              <data>

              </data>
              <imagesSet>
                      <image url="images/1.jpg"></image>
                      <image url="images/2.jpg"></image>
                      <image url="images/3.jpg"></image>   
              </imagesSet>
              <imagesSet>
                      <image url="images2/1.jpg"></image>
                      <image url="images2/2.jpg"></image>
                      <image url="images2/3.jpg"></image>
              </imagesSet>
              <imagesSet>
                      <image url="images3/1.jpg"></image>
                      <image url="images3/2.jpg"></image>
                      <image url="images3/3.jpg"></image>
              </imagesSet>

              • 4. Re: How to read XML?
                kglad Adobe Community Professional & MVP

                if you'll have a varying number of imagesets you need to fix your xml:

                 

                 

                 

                <data>

                </data>

                <imageSets>

                <imagesSet>
                        <image url="images/1.jpg"></image>
                        <image url="images/2.jpg"></image>
                        <image url="images/3.jpg"></image>   
                </imagesSet>
                <imagesSet>
                        <image url="images2/1.jpg"></image>
                        <image url="images2/2.jpg"></image>
                        <image url="images2/3.jpg"></image>
                </imagesSet>
                <imagesSet>
                        <image url="images3/1.jpg"></image>
                        <image url="images3/2.jpg"></image>
                        <image url="images3/3.jpg"></image>
                </imagesSet>

                </imageSets>

                1 person found this helpful
                • 5. Re: How to read XML?
                  TarunaR Level 1

                  You got it right, now it is loading at least however just the last item from the imageSet. I named all of them respectively, imageSet1, imageSet2, imageSet3.

                   

                  my code is the same. Please help :-(

                  • 6. Re: How to read XML?
                    kglad Adobe Community Professional & MVP

                    use:

                     

                    for(var i=0;i<yourxml.firstChild.nextSibling.childNodes.length;i++){
                    for(var j=0;j<yourxml.firstChild.nextSibling.childNodes[i].childNodes.length;j++){
                    trace(i+" "+j+" "+yourxml.firstChild.nextSibling.childNodes[i].childNodes[j]);
                    }
                    }

                    • 7. Re: How to read XML?
                      TarunaR Level 1

                      Nopes,

                       

                      You didnt get what i asked for.

                       

                      see, once i click on a button, it should fetch the content from xml to the movieclip(toolTipMC) on the stage, movieclip(toolTipMC) have a start button, once we click on that, it should fetch the very first image from the respective set(loc1, loc2, loc3) underneath there is next and previous button, when clicked on the next it should bring the next image from the same set.

                       

                      I am just stuck with the next and previous code.

                       

                      for(i=0;i<imgs.firstChild.nextSibling.childNodes.length;i++){
                      for(a=0;a<imgs.firstChild.nextSibling.childNodes[i].childNodes.length;a++){

                       

                      nextImg["loc"+i] = imgs.firstChild.nextSibling.childNodes[i].childNodes[a].attributes.url;

                       

                      }


                      a = 0;

                                  toolTipMC.previous.onRelease = function() {
                              
                                  if (a>0) {
                                      a--;
                                      toolTipMC.img.imgHolder.loadMovie(nextImg[myBtn._name]); //
                      myBtn._name are loc1, loc2, loc3
                                  
                               }


                              toolTipMC.next.onRelease = function() {

                       

                                newTotal = x.firstChild.nextSibling.childNodes[myBtn._name.charAt(3)].childNodes.length;

                                
                                  if (a<newTotal) {
                                      a++;
                                      toolTipMC.img.imgHolder.loadMovie(nextImg[myBtn._name]);

                       

                                  }

                      • 8. Re: How to read XML?
                        TarunaR Level 1

                        Hey thanks, however i got a way through. I did not run a second loop of j. I just divided myxml.firstChild.nextSibling.childNodes[i].childNodes[j] into two.


                        1.  myxml.firstChild.nextSibling.childNodes[i].childNodes

                        2. [j]

                         

                        so passing j value via my click and adding to my 1st.