6 Replies Latest reply on Apr 16, 2013 2:46 PM by CindyJones-Hulfachor

    Problem turning XML imported data to a number

    CindyJones-Hulfachor Level 1

      Asking for help, I am trying to turn XML data into a number that I can use in formulas in Animate. This is a simplified example of what I am trying to do. I have successfully retrieved the info from the xml file into the text on the stage. My problem is that I need to have the value of the imported data to be a number so that I can do future calculations with it, like add the two numbers together to make a new variable total. I have racked my brain reading about XML, spent several days trying obvious solutions to no avail, and yes I am new to coding although I have thoroughly tried many options.

      Thank you, Cindy J-H

       

      -----IMPORTING AN XML FILE "textimport.xml"

       

      <?xml version="1.0" encoding="UTF-8"?>

      <mediansaleschart>

       

      <figureone>20</figureone>

      <figuretwo>10</figuretwo>

       

      </mediansaleschart>

       

      ------EDGE ANIMATE CODING in Stage/creationComplete

       

      $.ajax({

       

                 type: "GET",

                 url: "textimport.xml",

                 dataType: "xml",

                 success: function(xml) {

       

      textOne = $(xml).find('figureone').text();

      textOneTxt.html(textOne);

      textTwo = $(xml).find('figuretwo').text();

      textTwoTxt.html(textTwo);

                 }

                 });

       

      var textOneTxt = $(this.lookupSelector("textOneTxt"));

      var textTwoTxt = $(this.lookupSelector("textTwoTxt"));

       

      ------EDGE ANIMATE ELEMENTS ON THE STAGE, text items

      textOneTxt

      textTwoTxt

        • 1. Re: Problem turning XML imported data to a number
          heathrowe Most Valuable Participant

          To get the value of the xml node you need to use parseInt.

          So for the total, I did something like this

                 myTotal = parseInt(figureone) + parseInt(figuretwo);

           

          You should also make use of the latest lookup reference

               instead of

                  $(this.lookupSelector("textOneTxt"));

               use this notation

                  sym.$("textOneTxt");

           

          Here is an example - http://www.heathrowe.com/edge/xmlnum/

          Download - http://www.heathrowe.com/edge/xmlnum/xmlnum.zip

           

          ////////// Code on compositionReady //////////////////////

           

          var figureone, figuretwo, myTotal;
          var outputField = sym.$("xmlOutput");
          var messageString;

           

          $.ajax({

          type: "GET",
          url: "sampleoutput.xml",
          dataType: "xml",
          success: function(xml) {
            figureone = $(xml).find('figureone').text();
            figuretwo = $(xml).find('figuretwo').text();
            myTotal = parseInt(figureone) + parseInt(figuretwo);
            messageString = "Figure One Value is: "+figureone+
              "<br>Figure Two Value is: "+figuretwo+
              "<br>Total Value is: "+myTotal;

            outputField.html(messageString);
            }
          });

          ///////////////// End Code ////////////////////////

           

          hth

          Darrell

          • 2. Re: Problem turning XML imported data to a number
            CindyJones-Hulfachor Level 1

            Darrell,

             

            Thank you so much. I have went through the download files that you made available and noted several areas where I had issues. Now I can move forward bringing the data into the other calculations that I have built. Have a great day!

             

            Cindy J-H

            • 3. Re: Problem turning XML imported data to a number
              heathrowe Most Valuable Participant

              Your welcome, Cindy.

               

              Darrell

              • 4. Re: Problem turning XML imported data to a number
                CindyJones-Hulfachor Level 1

                One additional concern: You have shown me how make xml data read as numbers inside the Ajax success function.

                Could you tell me how to make the variable myTotal read as a "Number" after it is sent out of the Ajax function.

                I plan on using retrieved xml data, outside ajax, to build  functions to run on many items.

                I would have thought I could have said:

                 

                Number(myTotal);

                var newTotal = myTotal *1;

                or parseInt(myTotal);

                 

                Thank you,

                Cindy

                • 5. Re: Problem turning XML imported data to a number
                  heathrowe Most Valuable Participant
                  Hi Cindy see additions (bold) and notes appended below to the original
                  code.
                  hth
                  Darrell
                   
                        var figureone, figuretwo, myTotal, myNewTotal;
                        var outputField = sym.$("xmlOutput");
                        var messageString;
                  
                            $.ajax({     
                                 type: "GET",
                                 url: "sampleoutput.xml",
                                 dataType: "xml",
                                 success: function(xml) {
                                           figureone = $(xml).find('figureone').text();
                                           figuretwo = $(xml).find('figuretwo').text();
                                           myTotal = parseInt(figureone) + parseInt(figuretwo);
                                           messageString = "Figure One Value is: "+figureone+
                                                     "Figure Two Value is: "+figuretwo+
                                                     "Total Value is: "+myTotal; 
                  
                                           outputField.html(messageString);
                                      
                                      customFunc(myTotal);                //  pass myTotal to a separate function
                                }
                           });
                  
                            function customFunc(myTotal) {       // separate function receives myTotal
                                     var newtotal = myTotal*3;        // manipulate myTotal - multiplied by 3 and store in var called newTotal
                                  sym.$("Text").html(newtotal);    // pass the new value to an element on the stage called "Text"
                                   alert (newtotal);                           // or alert the result to a popup window.
                            }
                  
                  
                  • 6. Re: Problem turning XML imported data to a number
                    CindyJones-Hulfachor Level 1

                    Darrell,

                     

                    Thank you for your help. I see how the math works outside of the success function. I know in time this will come easier, it gets clearer every day, I just need to have more patience.

                     

                    Cindy