6 Replies Latest reply on May 14, 2012 6:06 PM by Rothrock

    Pulling Specific Data from XML

    Claunchster Level 1

      I need to be able to use as2 to search through my XML file and populate dynamic text field with certain data. For example if this were my xml:

       

      <class>

           <person>

                <name>Last, First</name>

                <grade1> 98 </grade1>

                <grade2> 87</grade2>

                <grade3> 62 </grade3>

                <grade4> 93 </grade4>

                <grade5> 96 </grade5>

           </person>

           <person>

                <name>Last, First</name>

                <grade1> 98 </grade1>

                <grade2> 87</grade2>

                <grade3> 62 </grade3>

                <grade4> 93 </grade4>

                <grade5> 96 </grade5>

           </person>

      </class>

       

      Then I may want to search for the highest grade and fill in a dynamic text field, or maybe the second highest grade and pull that data, and the name of the person.  How could I do this?

       

      Thanks!

        • 1. Re: Pulling Specific Data from XML
          Ned Murphy Adobe Community Professional & MVP

          What you need to do is to read in your xml data, parse it into variables, and then process the parsed data.  For the data you show, I would probably parse each student as an object which has a name variable (or two for last and first names), and an array for the grades.  The student objects would be elements of an array.

           

          If you need info on parsing xml, try searching Google using terms like "AS2 XML tutorial"

          • 2. Re: Pulling Specific Data from XML
            Claunchster Level 1

            I've already done searches, but I wasn't able to find the kind of info I was looking for to search through and jerry-pick data from my everchangning xml, which is why I came to the discussion boards being an xml/flash novice of sorts it's usually pretty easy to find what I'm looking for.  I use the forums as a last resort.  Most of the info I need I can find in a tut somewhere else. Thanks all the same.

            • 3. Re: Pulling Specific Data from XML
              Ned Murphy Adobe Community Professional & MVP

              I was trying to tell you that you don't jerry-pick the data from the xml file.  You acquire the xml data from the file, parse it, then do your manipulations.

              • 4. Re: Pulling Specific Data from XML
                Rothrock Level 5

                The way your xml is structured is going to make it difficult to work with. By giving each "grade" node a number like that it will make it hard to loop through all the grade nodes. Just like you didn't call them <person1>, <person2>, etc. you should just have <grade> nodes. If you need to keep them sequences or tell them apart you can give them an attribute or something.

                 

                <class>

                     <person>

                          <name>Last, First</name>

                          <grade id="1"> 98 </grade>

                          <grade id="2"> 87</grade>

                          <grade id="3"> 62 </grade>

                          <grade id="4"> 93 </grade>

                          <grade id="5"> 96 </grade>

                     </person>

                     ....

                </class>

                 

                The built in tools for woring with XML is AS2 are pretty lame. So most folks convert the XML to something easier to work with Arrays or Objects. I find this (http://proto.layer51.com/d.aspx?f=1340) pretty useful.

                 

                If you have much xml work to do I would suggest learning AS3. The XML and XMLList in it are so much better.

                • 5. Re: Pulling Specific Data from XML
                  Claunchster Level 1

                  Unfortunately I'm stuck using AS2, here is what I'm using:

                   

                  //-------------------- AS2 --------------------

                  function loadXML(loaded) {

                   

                  if (loaded && typerank == "1"){

                   

                  _root.firstperiod = this.firstChild.childNodes[0].childNodes[0].firstChild.nodeValue;

                  _root.typerank= this.firstChild.childNodes[0].childNodes[6].firstChild.nodeValue;

                   

                  ffscore1_1_txt.text=_root.typingtest;

                  ffname1_1_txt.text = _root.firstperiod;

                  }else {

                    trace("file not loaded!");

                    }

                  }

                  xmlData = new XML();

                  xmlData.ignoreWhite = true;

                  xmlData.onLoad = loadXML;

                  xmlData.load("firstperiod.xml");

                  //-------------------- End AS2 --------------------

                   

                  When I use this my xml doesn't load.

                   

                  My xml actually looks like this:

                   

                  //-------------------- XML --------------------

                  <fp>

                                      <name>Last, First</name>

                                      <abyss>736</abyss>

                                      <abyssrank>6</abyssrank>

                                      <adventure>35498</adventure>

                                      <adventurerank>12</adventurerank>

                                      <typingtest>19</typingtest>

                                      <typerank>12</typerank>

                                      <abysslw>736</abysslw>

                                      <adventurelw>35498</adventurelw>

                                      <typingtestlw>19</typingtestlw>

                            </fp>

                  //-------------------- End XML --------------------

                   

                  I'm already using this xml to extrapolate data out of in other areas of the site and it works great. Now I just want to use it more in a different location to look at the "*rank" labels and see if they == 1, 2 or 3 and then fill in dynamic fields with the name, and score data.

                  • 6. Re: Pulling Specific Data from XML
                    Rothrock Level 5

                    Sorry I'm not understanding these two bits:

                     

                    When I use this my xml doesn't load.

                    I'm already using this xml to extrapolate data out of in other areas of the site and it works great.

                     

                    It seems that both can't be right?

                     

                    You should really download the XMLConstruct class I mentioned above. You could then use it like this:

                     

                    myData.fp.name[i].abyss._value

                     

                    or what have you. It is a lot easier to work with.

                     

                    Also is the rank like on a leaderboard or something? Wouldn't that be determined by the value of the associated node? In that case you don't really need it. If you do need it and it is associated with the other node of similar name then perhaps having it be an attribute?

                     

                    Do you know about for loops? They can be helpful. So you can do something like this:

                     

                    for(var i=0;i<5;i++){

                         trace(this.firstChild.childNodes[i].childNodes[0].firstChild.nodeValue;

                    }