2 Replies Latest reply on Mar 5, 2009 11:19 AM by timmydenty

    XML problem

    timmydenty
      Hi - I am currently using the great Kirupa xml slideshow tutorial, but am having issues as I have separated the slideshow images into different categories - ie. the xml will contain a number of client nodes, each with with a separate list of project nodes.

      I've just been trying to use 2 separate 'for loops', one counting client nodes and one counting project nodes. It seems to be pulling the data ok when I traced it, but I'm stuck on how to include both counts into the 3 arrays.

      As you can see I have tried using [ni] to no avail. Any ideas on how I could achieve this?

      AS:
      xmlNode = this.firstChild;
      clientname = [];
      imagedescription = [];
      imagepath = [];

      total = xmlNode.childNodes.length;
      subtotal = xmlNode.firstChild.childNodes.length;

      for (n=0; n<total; n++) {
      for (i=0; i<subtotal; i++) {
      clientname[ni] = xmlNode.childNodes[n].childNodes[0].firstChild.nodeValue;
      imagepath[ni] = xmlNode.childNodes[n].childNodes .childNodes[0].firstChild.nodeValue;
      imagedescription[ni] = xmlNode.childNodes[n].childNodes
      .childNodes[1].firstChild.nodeValue;
      }
      }

      XML:
        • 1. XML problem
          Ned Murphy Adobe Community Professional & MVP
          The way you are using [ni] won't fly, but [n][ i] might. You may want to rethink your storage structure.

          You have a client that has one name property, but multiple project properties. So you might want to try storing each client as an object. First capture the name as a var, then capture the projects as an array of objects, then store thos in another object (which is part of an overall array. Yeah, that's likely clear as mud.

          Here's another more visual attempt: clients is an array based on 'n', projects is an array of objects based on i

          clients[n] = { Name: x, projects: [ {img: a, desc: b},{img: y, desc: z},...] }

          O yeah, that's likely another beauty... anways, you can build the pieces and assemble them after each 'i' iteration is complete. The Name element is captured in the 'n' loop before the i loop begins, and the 'i' loop's project array is declared there as well. The project elements are collected as {img, desc} objects and pushed into the projects array in the 'i' loop. Between the end of the 'i' loop and the end of the 'n' loop, you build the client object and add it to the clients array

          You could also just store the images and descriptions as separate arrays within the client object if that's too confusing.

          clients[n] = { Name: x, images: [ imga, imgb,...], descs: [desca, descb,...] }
          • 2. XML problem
            timmydenty Level 1
            Thanks for thr reply. I managed to correct it by using [n] [ i] as you suggested, and by placing relevant code outside of the inner loop.

            total = xmlNode.childNodes.length;

            for (n=0; n<total; n++) {
            clientname[n] = xmlNode.childNodes[n].childNodes[0].firstChild.nodeValue;
            imagepath[n] = new Array();
            imagedescription[n] = new Array();
            extraimagepath[n] = new Array();
            subtotal = (xmlNode.childNodes[n].childNodes.length-1); // Subtract the client title node

            for (i=0; i<=subtotal; i++) {
            imagepath[n] = xmlNode.childNodes[n].childNodes.childNodes[0].firstChild.nodeValue;
            imagedescription[n] = xmlNode.childNodes[n].childNodes.childNodes[1].firstChild.nodeValue;
            extraimagepath[n] = xmlNode.childNodes[n].childNodes.childNodes[2].firstChild.nodeValue;
            }
            }