5 Replies Latest reply on Jul 30, 2009 3:07 PM by kglad

    XML usage question

    TaoHunter

      I'm currently working on a little project which requires the use of XML. Unfortunatly I don't know that much about xml and there isn't really that much time to figure out exactly how it works

       

      Below is a  part of what the xml contains:

      ----------------------------------------------------------------------

      <Jobs>
      <JobTotals>
           <JobClassSkill>TECHNOLOGY</JobClassSkill>
           <JobTypeIP>40</JobTypeIP>
           <JobTypeWS>4</JobTypeWS>
           <JobTypeZZP>23</JobTypeZZP>
      </JobTotals>

      <JobTotals>
           <JobClassSkill>PROCUREMENT</JobClassSkill>
           <JobTypeIP>2</JobTypeIP>
           <JobTypeWS>4</JobTypeWS>
           <JobTypeZZP>1</JobTypeZZP>
      </JobTotals>

      <JobTotals>
           <JobClassSkill>HRM</JobClassSkill>
           <JobTypeIP>2</JobTypeIP>
           <JobTypeWS>18</JobTypeWS>
           <JobTypeZZP>1</JobTypeZZP>
      </JobTotals>

      </Jobs>
      ----------------------------------------------------------------------


      What needs to be done is that for each JobClassSkill the 3 JobType amounts need to be added up and put into a variable or array

      so that I get something like:

      TECHNOLOGY = 67

      PROCUREMENT = 7

      HRM = 21

       

      Thanks in advance

        • 1. Re: XML usage question
          kglad Adobe Community Professional & MVP

          start with:

           

          var xml:XML=new XML();

          xml.ignoreWhite=true;

          xml.onLoad=parseXML;

           

          xml.load("yourxmlfile.xml");

           

          function parseXML(){

          trace(this.firstChild.childNodes);

          // should give you an array that you can loop through to parse the nodes.

          }

          1 person found this helpful
          • 2. Re: XML usage question
            TaoHunter Level 1

            Thanks for the quick response.

             

            The problem is that I'm not sure on how to parse the various nodes and how to identify to which variable they should belong so that I can add that value to the right variable.

            I assume it's done with a for() loop like this:

             

            function parseXML(){

            for (i=0; i<this.firstChild.childNodes.length;i++){

            }

            }

             

            But the question now is; What goes in the loop?

            • 3. Re: XML usage question
              kglad Adobe Community Professional & MVP

              correct:

              var obj:Object={};

               

              function parseXML() {

               

                  for (i=0; i<this.firstChild.childNodes.length; i++) {
                      var node:XMLNode = this.firstChild.childNodes[i];
                      obj[node.childNodes[0].firstChild.nodeValue]=Number(node.childNodes[1].firstChild.nodeVal ue)+Number(node.childNodes[2].firstChild.nodeValue)+Number(node.childNodes[3].firstChild.n odeValue);
                  }

               

              }

               

              // obj is an associative array that you can use.  for example,

               

              for(o in obj){

              trace(o+": "+obj[o]);

              }

              1 person found this helpful
              • 4. Re: XML usage question
                TaoHunter Level 1

                Well, it took me some time to figure it all out

                 

                At first the script wasn't doing anything at all. I kept getting undefined stuff and for loops that whern't running.

                But thanks to your helpfull posts I was able to figure out what was going wrong.

                 

                The script I ended up with is:

                 

                var xml:XML = new XML();
                xml.ignoreWhite = true;

                xml.load("test.xml");
                var jobs:Object = {};
                xml.onLoad = function(success) {
                    if (success) {
                        var xmllist = xml.firstChild;
                        for (i=0; i<xmllist.childNodes.length; i++) {
                            var node:XMLNode = xmllist.childNodes[i];
                            jobs[node.childNodes[0].firstChild.nodeValue] = Number(node.childNodes[1].firstChild.nodeValue)+Number(node.childNodes[2].firstChild.node Value)+Number(node.childNodes[3].firstChild.nodeValue);
                      }
                    }
                };

                 

                this results in:

                jobs.TECHNOLOGY = 67

                jobs.PROCUREMENT = 7

                jobs.HRM = 21


                I've changed the onLoad to a function in it self and the thing that was missing in the script was the line

                var xmllist = xml.firstChild;

                After inserting that line things where finally comming together

                 

                Thank you very much for the help Kglad and I'm, sure klgad.com services are running smoothly

                 

                And now I can't wait to get to work again tomorrow to put things into action...

                • 5. Re: XML usage question
                  kglad Adobe Community Professional & MVP

                  you're welcome.