Skip navigation
Currently Being Moderated

XML Data Search

May 11, 2012 7:27 AM

Tags: #cs5.5 #xml #actionscript3

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

 

<firstperiod>

     <person>

          <name>Last, First</name>

          <grade1> 98 </grade1>

          <grade2> 87</grade2>

          <grade3> 62 </grade3>

          <grade4> 93 </grade4>

          <grade5> 96 </grade5>

     </person>

     <person2>

          <name>Last, First</name>

          <grade1> 98 </grade1>

          <grade2> 87</grade2>

          <grade3> 62 </grade3>

          <grade4> 93 </grade4>

          <grade5> 96 </grade5>

     </person2>

</firstperiod>

 

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.  How could I do this?

 

Thanks!

 
Replies
  • Currently Being Moderated
    May 11, 2012 7:44 AM   in reply to Claunchster

    that's not a particularly good use of xml

     

    you might want to consider using something like this instead

     

    <period type="first">

              <person>

                        <name>Last, First</name>

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

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

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

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

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

              </person>

              <person>

                        <name>Last, First</name>

                        <grade type="1"> 89 </grade>

                        <grade type="2"> 78 </grade>

                        <grade type="3"> 26 </grade>

                        <grade type="4"> 39 </grade>

                        <grade type="5"> 69 </grade>

              </person>

    </period>;

     
    |
    Mark as:
  • Currently Being Moderated
    May 11, 2012 7:48 AM   in reply to Claunchster

    well I'm not particularly good either but I'll try

     

    <person> and <person2> are representing the same data therefore they should be represented by the same tag

     

    similarly for grade1 - 5

     

    if you need to differentiate between different grades they should have a property, like subject, or test so you know which subject or test the grades were for (I chose type in the example I gave just to be a bit more generic)

     

    similarly i changed firstperiod to period, because first implies there will be more and they are all periods so again i gave them a property so you could tell which one was first period and so on

     
    |
    Mark as:
  • Currently Being Moderated
    May 11, 2012 7:55 AM   in reply to _spoboyle

    in answer to your original question this might point you in the right direction

     

    package

    {

              import flash.display.Sprite;

     

              public class Main extends Sprite

              {

                        private static const xml:XML =

                                  <period type="first">

                                            <person>

                                                      <name>Last, First</name>

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

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

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

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

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

                                            </person>

                                            <person>

                                                      <name>Last, First</name>

                                                      <grade type="1"> 89 </grade>

                                                      <grade type="2"> 78 </grade>

                                                      <grade type="3"> 26 </grade>

                                                      <grade type="4"> 39 </grade>

                                                      <grade type="5"> 69 </grade>

                                            </person>

                                  </period>;

     

                        public function Main()

                        {

                                  var first:int = int.MIN_VALUE;

                                  var second:int = int.MIN_VALUE;

     

                                  var grades:XMLList = xml.descendants("grade");

     

                                  for each (var grade:XML in grades)

                                  {

                                            var gradeValue:int = parseInt(grade);

                                            if (gradeValue > first)

                                            {

                                                      second = first;

                                                      first = gradeValue;

                                            }

                                            else if (gradeValue > second)

                                            {

                                                      second = gradeValue;

                                            }

                                  }

     

                                  trace("highest: " + first);

                                  trace("second highest: " + second);

                        }

     

              }

     

     

    }

     
    |
    Mark as:
  • Currently Being Moderated
    May 14, 2012 2:16 AM   in reply to Claunchster

    I don't really work in AS2 so you might get a better response on the AS2 forum but this is something i knocked up that seems to work

     

    grades.xml

     

    <period type="first">

                        <person>

                                              <name>Last, First</name>

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

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

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

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

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

                        </person>

                        <person>

                                              <name>Last, First</name>

                                              <grade type="1"> 89 </grade>

                                              <grade type="2"> 78 </grade>

                                              <grade type="3"> 26 </grade>

                                              <grade type="4"> 39 </grade>

                                              <grade type="5"> 69 </grade>

                        </person>

    </period>

    <period type="second">

                        <person>

                                              <name>Last, First</name>

                                              <grade type="1"> 56 </grade>

                                              <grade type="2"> 49 </grade>

                                              <grade type="3"> 78 </grade>

                                              <grade type="4"> 99 </grade>

                                              <grade type="5"> 43 </grade>

                        </person>

                        <person>

                                              <name>Last, First</name>

                                              <grade type="1"> 24 </grade>

                                              <grade type="2"> 56 </grade>

                                              <grade type="3"> 73 </grade>

                                              <grade type="4"> 81 </grade>

                                              <grade type="5"> 55 </grade>

                        </person>

    </period>

     

    as in frame 1 of fla

     

    trace("start");

    var xml:XML = new XML();

    xml.ignoreWhite = true;

    xml.load("grades.xml");

    xml.onLoad = xmlLoaded;

     

     

    function xmlLoaded(loaded)

    {

              if (loaded)

              {

                        var grades:Array = getGrades(this);

     

                        var first:Number = Number.MIN_VALUE;

                        var second:Number = Number.MIN_VALUE;

     

                        for (var i:Number = 0; i < grades.length; i++)

                        {

                                  var grade:Number = grades[i];

                                  if (grade > first)

                                  {

                                            second = first;

                                            first = grade;

                                  }

                                  else if (grade > second)

                                  {

                                            second = grade;

                                  }

                        }

     

                        trace("first: " + first);

                        trace("second: " + second);

              }

    }

     

     

    function getGrades(xml:XML):Array

    {

              var gradeValues:Array = new Array();

     

              var periods:Array = xml.childNodes;

              for (var i:Number = 0; i < periods.length; i++)

              {

                        var period:XMLNode = periods[i];

     

                        var people:Array = period.childNodes;

                        for (var j:Number = 0; j < people.length; j++)

                        {

                                  var person:XMLNode = people[j];

     

                                  var grades:Array = person.childNodes;

                                  for (var k:Number = 0; k < grades.length; k++)

                                  {

                                            var grade:XMLNode = grades[k];

                                            if (grade.nodeName == "grade")

                                            {

                                                      var gradeValue:Number = parseInt(grade.firstChild.toString());

                                                      gradeValues.push(gradeValue);

                                            }

                                  }

                        }

              }

     

              return gradeValues;

    }

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points