26 Replies Latest reply on May 17, 2009 6:00 PM by Gregory Lafrance

    Quiz App - What is the Best Way to Represent Data?

    Gregory Lafrance Level 6

      I'm creating a quiz app, kind of like flash cards.

       

      The text for the questions and answers might contain HTML code, double and single quotes, etc..

       

      The text for a question or answer might also extend across multiple lines, with some lines indented.

       

      Should I store the question and answer text in an XML file, or in text files?

       

      I ask because I know sometimes when you store data in XML files you have to jump through hoops to have Flex not mess up the data if it contains <, >, ", ', and lots of whitespace.

       

      If I store the data in an XML file, I guess I need to escape < > " ' and unescape after reading in the data?

        • 1. Re: Quiz App - What is the Best Way to Represent Data?
          -Hob

          I take it this is an air app and you're just looking to store you data locally as opposed to sending it over an http service?  If so, you might think about storing it as a bytearray.  Adobe's collection objects all implement IExternalizable, so you should be able to do something like this:

           

          var stream:FileStream = new FileStream();
          var file:File File.applicationStorageDirectory.resolvePath('myfile.txt');
          stream.open(customersFile, FileMode.WRITE);
          myCollection.writeExternal(stream);
          stream.close();
          
          1 person found this helpful
          • 2. Re: Quiz App - What is the Best Way to Represent Data?
            Gregory Lafrance Level 6

            Actually the Flex app will read in the file from the web using HTTPservice.

            • 3. Re: Quiz App - What is the Best Way to Represent Data?
              ntsiii Level 3

              Greg, I would suggest that you use escape() to encode the string when you save it, and unescape() when you want to display it.  These functions handle the normal xml special characters but also whitespace.

               

              In fact, htmlText will take an escaped string directly, without explicit unescape.

               

              The downside to this approach is that the escaped string is very difficult to read or directly edit (so is a bytearray!).

               

              Tracy Spratt

              • 4. Re: Quiz App - What is the Best Way to Represent Data?
                Gregory Lafrance Level 6

                Tracy, so I should use escape() before I write out to the XML file, and then unescape() when I read that XML file in?

                 

                I am using an AIR app to provide a UI to easily enter questions and save to an XML file.

                 

                Then in my Flex quiz app I read in the XML file and use e4x syntax to parse the question, answers, etc.

                 

                So I guess I escape() before writing to XML and unescape() after reading in the XML?

                • 5. Re: Quiz App - What is the Best Way to Represent Data?
                  ntsiii Level 3

                  Yes, escape() the string before writing it into the xml file.  I have used this with both attributes and text nodes.

                   

                  If you are gong to display the string through a text.htmlText property you do not need to unescape() it first, the htmlText will do that.  If it is XML and you need to parse it via XML(), then you need to unescape() it first.

                   

                  Tracy

                  • 6. Re: Quiz App - What is the Best Way to Represent Data?
                    Gregory Lafrance Level 6

                    Okay Tracy, I guess that's the answer.

                     

                    I'm building an app to help others prepare for taking the Flex ACE certification exam, so there could be code examples with MXML tags, HTML tags, CDATA blocks, double and single quotes, etc.

                     

                    I want to have my question creation system solid before I start entering questions because I don't want to enter 1500 questions and find out something is wrong.

                     

                    Thanks again.

                    • 7. Re: Quiz App - What is the Best Way to Represent Data?
                      John Hall Level 4

                      If it's XML, wouldn't it work OK if you put it in CDATA tags?

                       

                      <Info>
                              <ID>intro_1</ID>
                              <caption>
                                  <![CDATA[Module 1: ''How the West was not Won'']]>
                              </caption>
                          </Info>

                      1 person found this helpful
                      • 8. Re: Quiz App - What is the Best Way to Represent Data?
                        Gregory Lafrance Level 6

                        I'm not sure, that's why I posted.

                         

                        I'll add your hint to list of things to try, to see what works best.

                         

                        Thanks!

                        • 9. Re: Quiz App - What is the Best Way to Represent Data?
                          ntsiii Level 3

                          Yes, that would work, but I have read of many folks having difficulty with

                          CDATA blocks.  Also you would have to have the data in a text node to use

                          CDATA, not an attribute.

                           

                          Tracy Spratt,

                          Lariat Services, development services available

                          • 11. Re: Quiz App - What is the Best Way to Represent Data?
                            ntsiii Level 3

                            Hey, everyone, I was mistaken when I said that htmlText would automatically

                            unescape the string.

                             

                            You DO need to unescape() it before sending it to the htmlText property.

                             

                            Tracy Spratt,

                            Lariat Services, development services available

                            • 12. Re: Quiz App - What is the Best Way to Represent Data?
                              Gregory Lafrance Level 6

                              A new wrinkle. When my data gets to PHP it breaks my code there because it contains something the PHP code does not like. I'm sending it as JSON, but it is already unescaped at that point, and I am trying to add it to MySQL tables.

                               

                              I know this is not a Flex question, but data manipulation related. How can I process data that has <, >, ", ', and CDATA blocks like sample MXML code in PHP and insert into MySQL without problems due to these characters?

                               

                              Thanks in advance!

                              • 13. Re: Quiz App - What is the Best Way to Represent Data?
                                ntsiii Level 3

                                Hmm, I would have thought you were just manipulating and saving strings.

                                 

                                Do you have to parse the encoded strings on the server?  If so you need to

                                unescape them there befor yo parse them.  I found the .net function that is

                                the equivalent of unescape, I am not sure whet it would be in php.

                                 

                                Tracy Spratt,

                                Lariat Services, development services available

                                • 14. Re: Quiz App - What is the Best Way to Represent Data?
                                  Michael Borbor Level 4

                                  I don't know if MySQL has the XML datatype as SQL Server has, but if that's the case you can store XML directly without worrying about < >. And since you're using Flex/PHP you could use ZendAMF better.

                                  • 15. Re: Quiz App - What is the Best Way to Represent Data?
                                    Gregory Lafrance Level 6

                                    I unescape the text in PHP and then try to insert into MySQL, but something is going wrong. I'll have to investigate further.

                                    • 16. Re: Quiz App - What is the Best Way to Represent Data?
                                      Gregory Lafrance Level 6

                                      I've heard about Zend, but I just don't want to have to start learning another new technology right now, then again, I have to learn PHP...

                                      • 17. Re: Quiz App - What is the Best Way to Represent Data?
                                        Michael Borbor Level 4

                                        Greg isn't learning a new tech is just setting a few thing and you're ready to use RemoteObject and as you know they perform better, look this very simple tutorial I don't like at all PHP but seems so simple.

                                         

                                        http://corlan.org/2008/11/13/flex-and-php-remoting-with-zend-amf/

                                        • 18. Re: Quiz App - What is the Best Way to Represent Data?
                                          Gregory Lafrance Level 6

                                          Very kool Michael. I have a feeling you'll save me a lot of time with this. Thanks VERY much!

                                          • 20. Re: Quiz App - What is the Best Way to Represent Data?
                                            Gregory Lafrance Level 6

                                            It appears that my web hosting company might have to have the Zend Framework 1.7 installed on my web server, but I am hosted through a web hosting company. Don't know if that is a deal breaker for using Zend.

                                            • 21. Re: Quiz App - What is the Best Way to Represent Data?
                                              Michael Borbor Level 4

                                              That could be a problem, your web hosting tech support will give you the answer, but as Flex Apps are getting more and more popular most likely the web hosting companies will begin installing ZendFramework more and more

                                              • 22. Re: Quiz App - What is the Best Way to Represent Data?
                                                Gregory Lafrance Level 6

                                                Actually, I spoke too soon. I used zend_version() and found that version 2.2 is currently supported, so I'll assume that although the tutorial you provided mentions Zend Framework 1.7, if my web hosting company has 2.2 then I'm kool.

                                                 

                                                Sorry for the fire alarm.

                                                • 23. Re: Quiz App - What is the Best Way to Represent Data?
                                                  Michael Borbor Level 4

                                                  Oh you see what I meant, yes the tutorial is a bit old, anyway you're ready

                                                  to go.

                                                  • 24. Re: Quiz App - What is the Best Way to Represent Data?
                                                    Gregory Lafrance Level 6

                                                    Just thought I'd give you all an update.

                                                     

                                                    I haven't implemented using Zend yet. I've been investigating exactly what is going on with my data in PHP/JSON.

                                                     

                                                    It seems some characters are causing problems and are not escaped by the Flex escape() method:

                                                    <    >    "

                                                     

                                                    So I guess I need to write a function to replace these characters with their HTML entities.

                                                     

                                                    I don't know if Zend eliminates the need to do this replace.

                                                     

                                                    This backend data stuff seems a bit scary. Have I thought of all the possible characters that could cause problems?

                                                     

                                                    Hard to say I guess.

                                                    • 25. Re: Quiz App - What is the Best Way to Represent Data?
                                                      ntsiii Level 3

                                                      Huh?  I am sure escape() encodes those characters.  Have you traced out a

                                                      string on the Flex side to be sure?

                                                       

                                                      Tracy Spratt,

                                                      Lariat Services, development services available

                                                      • 26. Re: Quiz App - What is the Best Way to Represent Data?
                                                        Gregory Lafrance Level 6

                                                        If I enter this text and send it using JSON to PHP:

                                                         

                                                         

                                                        What does this code do?
                                                        
                                                        <?xml version="1.0" encoding="utf-8"?>
                                                        <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
                                                          <mx:Script>
                                                            <![CDATA[
                                                              private function testFunc(testArg:String):int{
                                                                var myVar:String = "#000000";
                                                                var myVar2:int = 0xFFFFFF;
                                                                return 1;
                                                              }
                                                            ]]>
                                                          </mx:Script>
                                                        </mx:Application>
                                                        


                                                        This is the raw, escaped data received by my PHP script:

                                                         

                                                        %7B%22questionData%22%3A%7B%22answerDescription%22%3A%22test%20question%20description%22%2 C%22questionAnswers%22%3A%5B%7B%22answer%22%3A%22test%20answer1%22%2C%22correct%22%3A1%7D% 2C%7B%22answer%22%3A%22test%20answer2%22%2C%22correct%22%3A0%7D%5D%2C%22questionMetadata%2 2%3A%5B%7B%22metadata%22%3A%22test%22%7D%5D%2C%22questionText%22%3A%22What%20does%20this%2 0code%20do%3F%5Cr%5Cr%3C%3Fxml%20version%3D%5C%221.0%5C%22%20encoding%3D%5C%22utf-8%5C%22% 3F%3E%5Cr%3Cmx%3AApplication%20xmlns%3Amx%3D%5C%22http%3A//www.adobe.com/2006/mxml%5C%22%3 E%5Cr%20%20%3Cmx%3AScript%3E%5Cr%20%20%20%20%3C%21%5BCDATA%5B%5Cr%20%20%20%20%20%20private %20function%20testFunc%28testArg%3AString%29%3Aint%7B%5Cr%20%20%20%20%20%20%20%20var%20myV ar%3AString%20%3D%20%5C%22%23000000%5C%22%3B%5Cr%20%20%20%20%20%20%20%20var%20myVar2%3Aint %20%3D%200xFFFFFF%3B%5Cr%20%20%20%20%20%20%20%20return%201%3B%5Cr%20%20%20%20%20%20%7D%5Cr %20%20%20%20%5D%5D%3E%5Cr%20%20%3C/mx%3AScript%3E%5Cr%3C/mx%3AApplication%3E%22%2C%22quest ionType%22%3A%22Multiple%20Choice%22%7D%7D

                                                         

                                                        And this is the data after urldecode(), str_replace(), and utf8_encode():

                                                         

                                                        {"questionData":{"answerDescription":"test question description","questionAnswers":[{"answer":"test answer1","correct":1},{"answer":"test answer2","correct":0}],"questionMetadata":[{"metadata":"test"}],"questionText":"What does this code do?rr<?xml version="1.0" encoding="utf-8"?>r<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">r  <mx:Script>r    <![CDATA[r      private function testFunc(testArg:String):int{r        var myVar:String = "#000000";r        var myVar2:int = 0xFFFFFF;r        return 1;r      }r    ]]>r  </mx:Script>r</mx:Application>","questionType":"Multiple Choice"}}


                                                        My PHP script is not able to parse this and get data from these objects:

                                                         

                                                        $questionData = $dataRaw["questionData"];
                                                        $questionType = $dataRaw["questionData"]["questionType"];
                                                        $questionText = $dataRaw["questionData"]["questionText"];

                                                         

                                                        The actual problem is that $questionData does not contain anything.


                                                        However, if I enter this text and send it using JSON to PHP:

                                                         

                                                        What does this code do?

                                                         

                                                        &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;&lt;mx:Application xmlns:mx=&quot;http://www.adobe.com/2006/mxml"><mx:Script><![CDATA[private function testFunc(testArg:String):int{var myVar:String = &quot;#000000&quot;;var myVar2:int = 0xFFFFFF;return 1;}]]&gt;&lt;/mx:Script&gt;&lt;/mx:Application&gt;

                                                         

                                                        This is the raw, escaped data received by my PHP script:

                                                         

                                                        %7B%22questionData%22%3A%7B%22answerDescription%22%3A%22test%20question%20description%22%2 C%22questionAnswers%22%3A%5B%7B%22answer%22%3A%22test%20answer1%22%2C%22correct%22%3A1%7D% 2C%7B%22answer%22%3A%22test%20answer2%22%2C%22correct%22%3A0%7D%5D%2C%22questionMetadata%2 2%3A%5B%7B%22metadata%22%3A%22test%22%7D%5D%2C%22questionText%22%3A%22What%20does%20this%2 0code%20do%3F%5Cr%5Cr%26lt%3B%3Fxml%20version%3D%26quot%3B1.0%26quot%3B%20encoding%3D%26qu ot%3Butf-8%26quot%3B%3F%26gt%3B%26lt%3Bmx%3AApplication%20xmlns%3Amx%3D%26quot%3Bhttp%3A// www.adobe.com/2006/mxml%26quot%3B%26gt%3B%26lt%3Bmx%3AScript%26gt%3B%26lt%3B%21%5BCDATA%5B private%20function%20testFunc%28testArg%3AString%29%3Aint%7Bvar%20myVar%3AString%20%3D%20% 26quot%3B%23000000%26quot%3B%3Bvar%20myVar2%3Aint%20%3D%200xFFFFFF%3Breturn%201%3B%7D%5D%5 D%26gt%3B%26lt%3B/mx%3AScript%26gt%3B%26lt%3B/mx%3AApplication%26gt%3B%22%2C%22questionTyp e%22%3A%22Multiple%20Choice%22%7D%7D

                                                         

                                                        And this is the data after urldecode(), str_replace(), and utf8_encode():

                                                         

                                                        {"questionData":{"answerDescription":"test question description","questionAnswers":[{"answer":"test answer1","correct":1},{"answer":"test answer2","correct":0}],"questionMetadata":[{"metadata":"test"}],"questionText":"What does this code do?rr&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;&lt;mx:Application xmlns:mx=&quot;http://www.adobe.com/2006/mxml"><mx:Script><![CDATA[private function testFunc(testArg:String):int{var myVar:String = &quot;#000000&quot;;var myVar2:int = 0xFFFFFF;return 1;}]]&gt;&lt;/mx:Script&gt;&lt;/mx:Application&gt;","questionType":"Multiple Choice"}}

                                                         

                                                        And my PHP script processes the data successfully. In this case $questionData correctly contains the data.


                                                        I've diffed the raw data and JSON data received in my PHP script, and the &quot; and &lt; and &gt; are there, but if I do not escape them manually as we have seen it does not work.

                                                         

                                                        On the Flex side this is how I process the data:

                                                        private function saveQuestion(saveOrUpdate:String):void{
                                                        
                                                          var questionObj:Object = {    questionData: {
                                                              questionType: quesType.selectedItem, 
                                                              questionText: questionTxt.text,
                                                              answerDescription: answerDescTxt.text,
                                                              questionAnswers: getAnswers(),
                                                              questionMetadata: getMetadata()
                                                            }
                                                          }
                                                        
                                                        
                                                        
                                                        
                                                          var requestSend:Object = new Object();
                                                        
                                                          var dataString:String = JSON.encode(questionObj);
                                                          dataString = escape(dataString);
                                                          requestSend.questionDataString = dataString;
                                                          requestSend.addQuestion = 
                                                        "true";  addQuestionRequest.send(requestSend); 
                                                        
                                                        }