22 Replies Latest reply: Aug 27, 2012 7:54 AM by Ned Murphy RSS

    Please, give me an advise how to make a search of a number in xml-file (as3.0)

    createflash@mail.ru

      Hello! Sorry for troubling.

      Please, give me an advice how to make a search of a number in xml-file (as3.0)

      Thank you!

        • 1. Re: Please, give me an advise how to make a search of a number in xml-file (as3.0)
          Ned Murphy CommunityMVP

          Since xml files are text files, you need to examine the individual characters in the data to see if they are numeric (0-9).

          • 2. Re: Please, give me an advise how to make a search of a number in xml-file (as3.0)
            createflash@mail.ru Community Member

            Thank you for your advice!

            Ned Murphy wrote:

             

            Since xml files are text files, you need to examine the individual characters in the data to see if they are numeric (0-9).

            • 3. Re: Please, give me an advise how to make a search of a number in xml-file (as3.0)
              sinious CommunityMVP

              Can you post a sample of the XML and let us know where in the XML you're interested in? If you only search it as a long string then searching for the number 0, 1 or 8 can be found in the XML declaration itself:

               

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

               

              A sample of the XML would be best.

              • 4. Re: Please, give me an advise how to make a search of a number in xml-file (as3.0)
                createflash@mail.ru Community Member

                Thank you for advices and help, but I do not quite understand the mechanism of interaction as3.0 and xml file when the user checks, for example, your phone number in the receipt of the xml data file.

                • 5. Re: Please, give me an advise how to make a search of a number in xml-file (as3.0)
                  createflash@mail.ru Community Member

                  Hello! I tried to write, but it's writing me always that it can't find a number ("false"):

                   

                  This code:

                   

                   

                  import flash.events.Event;

                  import flash.net.URLLoader;

                  import flash.net.URLRequest;

                  var xmlFileToSearch:XML;

                  var loader:URLLoader = new URLLoader();

                  var request:URLRequest = new URLRequest("xmlFileToSearch.xml");

                  loader.load(request);

                  loader.addEventListener(Event.COMPLETE, onComplete);

                  function onComplete(event:Event):void

                  {

                            var loader:URLLoader = event.target as URLLoader;

                            if (loader != null)

                            {

                                      xmlFileToSearch = new XML(loader.data);

                                      trace(xmlFileToSearch.toXMLString());

                            }

                            else

                            {

                                      trace("loader is not a URLLoader!");

                            }

                  }

                  trace(xmlFileToSearch);

                  searchInput.text = "enter the receipt number";

                  searchInput.restrict = "0-9";

                  searchDisplay.text = "Search Results";

                   

                  searchButton.visible = true;

                  searchButton.addEventListener(MouseEvent.MOUSE_DOWN, searchXMLFile);

                   

                  function searchXMLFile(Event:MouseEvent):void

                  {

                            searchDisplay.text = "";

                   

                            var pageList:XMLList = xmlFileToSearch.receipt.number;

                            var m_sSearchTerm:String = searchInput.text;

                   

                            var xmlCount:Number = 0;

                            for (var i:int = 0; i < pageList.length(); i++)

                            {

                                      var item:XML = pageList[i];

                                      var textList:XMLList = item.text();

                                      xmlCount++;

                                      for (var j:int = 0; j < textList.length(); j++)

                                      {

                                                var t:XML = textList[j];

                                                if (t.toString().indexOf(m_sSearchTerm) != -1)

                                                {

                                                          searchDisplay.text = "true";

                                                }

                                                searchDisplay.text = "false";

                                      }

                            }

                  }

                  my xml  fale:

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

                  <base>

                  <receipt>

                     <number>     12345678          </number>

                  <number>     987876          </number>

                  <number>     678678         </number>

                  <number>     45645636          </number>

                  <number>     57567          </number>

                  <number>    12423434          </number>

                       

                  </receipt>

                  </base>

                   

                  Please, maybe anyone could correct me. Thank you.

                  • 6. Re: Please, give me an advise how to make a search of a number in xml-file (as3.0)
                    Ned Murphy CommunityMVP

                    My response was based on your question of finding numbers anywhere in the data of an xml file.  If you know the data is numeric data and the names of the nodes containing that data, then you do not have to check characters and can just check the number data using the string values....

                     

                    function searchXMLFile(Event:MouseEvent):void
                    {
                              searchDisplay.text = "";
                              var pageList:XMLList = xmlFileToSearch.receipt.number;
                              searchDisplay.text = "false";
                       
                              for (var i:int = 0; i < pageList.length(); i++)
                              {

                                   if(pageList[i] == searchInput.text){
                                       searchDisplay.text = "true";
                                   }

                              }
                    }

                     

                    Even if you were looking for individual numeric characters in your previous code you would always have ended up with a false output due to have the "false" line always canceling the true line...

                     

                                                  if (t.toString().indexOf(m_sSearchTerm) != -1)

                                                  {

                                                            searchDisplay.text = "true";

                                                  }

                                                  searchDisplay.text = "false";  // this will always overwrite the textfield

                    • 7. Re: Please, give me an advise how to make a search of a number in xml-file (as3.0)
                      createflash@mail.ru Community Member

                      Thank you! Thank you! Thank you very much! Thank you for your explain, i'm so appreciate!

                      • 9. Re: Please, give me an advise how to make a search of a number in xml-file (as3.0)
                        createflash@mail.ru Community Member

                        Hello again and again! I'm so sorry for troubling you again and sorry for my incomprehension.

                        I tried to complicate the task of (as my client wants) and again run into incomprehension of problem: why can not convert "" in XMLList. and identify at number 78 and 9? Please, explain to me one more time.

                        Sorry again and thank you.

                         

                         

                        This code:

                         

                        function searchXMLFile(Event:MouseEvent):void

                        {

                                  searchDisplay.text = "";

                                  statusDisplay.text = "";

                                  var pageList:XMLList = xmlFileToSearch.receipt.number;

                                  var status:XMLList = xmlFileToSearch.receipt.status;

                                  searchDisplay.text = "false";

                         

                                  for (var i:int = 0; i < pageList.length(); i++)

                                  {

                                            var model:XMLList = pageList[i].model.toString();

                                            var imei:XMLList = pageList[i].imei.toString();

                                            var number:XMLList = pageList[i].number.toString();

                                            if (pageList[i] == searchInput.text)

                                            {

                                                      searchDisplay.text = model + imei + number;

                         

                                                      var t = status;

                         

                                                      if (t.toString().indexOf(status) != 78)

                                                      {

                         

                                                                statusDisplay.text = "ready";

                                                      }

                                                      else if (t.toString().indexOf(pageList[i].status) != 9)

                                                      {

                         

                                                                statusDisplay.text = "return";

                                                      }

                                                      else

                                                      {

                                                                statusDisplay.text = "underway";

                                                      }

                                            }

                         

                                  }

                        }

                        my xml  file

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

                        <base>

                        <receipt>

                           <number>     12345678          </number>

                        <model>Sony</model>

                        <status> 0</status>

                        <imei>44444t</imei>

                        </receipt>

                         

                        <receipt>

                           <number>     56565656         </number>

                        <model>panasonic</model>

                        <status> 9</status>

                        <imei>44444t</imei>

                        </receipt>

                         

                        <receipt>

                           <number>     345353453        </number>

                        <model>Sony</model>

                        <status>78</status>

                        <imei>44444t</imei>

                        </receipt>

                         

                        <receipt>

                           <number>     8989898          </number>

                        <model>Sony</model>

                        <status> 9</status>

                        <imei>44444t</imei>

                         

                        </receipt>

                         

                         

                        </base>

                        • 10. Re: Please, give me an advise how to make a search of a number in xml-file (as3.0)
                          Ned Murphy CommunityMVP

                          You need to learn how to use the trace() function to track down why things process or not.

                           

                          You dont understand what the indexOf method is doing.  In the following line....

                           

                               if (t.toString().indexOf(status) != 78)

                           

                          The code is checking if the index of whatever "status" is within whatever "t" is is 78.  I would guess it is unlikely that will ever be.  If you only want to check if whatever "status" is is a portion of the String that "t" is you would use...

                           

                               if (t.toString().indexOf(status) != -1)

                           

                          The -1 indicates that status is not contained in t.

                           

                          If you wanted to detect if "78" CONTAINS the status value (the value could be 2227865 and be valid), then you would use....

                           

                               if (status.indexOf("78") != -1)

                           

                          If you wanted to check if "78" IS the status value, you would use...

                           

                               if (status == "78")

                          • 11. Re: Please, give me an advise how to make a search of a number in xml-file (as3.0)
                            Andrei1 Community Member

                            You don't have to jump through all these hoops, loops and conditionals. The following code will do the job:

                             

                            function searchXMLFile(e:MouseEvent):void
                            {
                                  // get node which <number> node value is the serach value
                                  var node:XMLList = xml.receipt.(number == searchInput.text);
                                  searchDisplay.text = node.model.text() + node.imei.text() + node.number.text();
                                  // cast to integer - node text is String
                                  switch(int(node.status)) {
                                        case 78:
                                              statusDisplay.text = "ready";
                                         break;
                               
                                        case 9:
                                              statusDisplay.text = "return";
                                         break;
                               
                                        default:
                                              statusDisplay.text = "underway";
                                         break;
                                 }
                            }
                            
                            
                            • 12. Re: Please, give me an advise how to make a search of a number in xml-file (as3.0)
                              createflash@mail.ru Community Member

                              Thank you again for your understandable explain, attention to my problem and help, code is logical and will have to work, any mistakes doesn't shown and "searchDisplay.text" is empty, but it doesn't work. Sorry.

                              • 13. Re: Please, give me an advise how to make a search of a number in xml-file (as3.0)
                                Andrei1 Community Member

                                I forgot the situation when result is not found. Here is the code that remedies these cases:

                                 

                                 

                                function searchXMLFile(e:MouseEvent):void
                                {
                                      searchDisplay.text = "false";
                                      statusDisplay.text = "";
                                      // get node which <number> node value is the serach value
                                      var node:XMLList = xmlFileToSearch.receipt.(number == searchInput.text);
                                      // presence of number indicates that result is found
                                      if (Boolean(int(node.number)))
                                      {
                                            searchDisplay.text = node.model.text() + node.imei.text() + node.number.text();
                                            // cast to integer - node text is String
                                            switch (int(node.status))
                                            {
                                                    case 78: 
                                                         statusDisplay.text = "ready";
                                                    break;
                                   
                                                    case 9: 
                                                         statusDisplay.text = "return";
                                                    break;
                                   
                                                    default: 
                                                         statusDisplay.text = "underway";
                                                    break;
                                          }
                                     }
                                }
                                
                                

                                 

                                Replced var xml with xmlFileToSearch to match your conventions

                                • 14. Re: Please, give me an advise how to make a search of a number in xml-file (as3.0)
                                  createflash@mail.ru Community Member

                                  THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! YOU SAVE ME, GUYS! I ADMIRE YOU, YOU'RE GREAT MAN!

                                  • 15. Re: Please, give me an advise how to make a search of a number in xml-file (as3.0)
                                    Andrei1 Community Member

                                    I have noticed some inefficiencies in your code:

                                     

                                    1. You should name variables with lower case because by convention anything that starts with upper case refers to Class. So, instead of "Even" it should be "event"

                                    2. Event with upper case is name of the AS3 class - you should be careful with reserved words.

                                    3. The following code is more efficient (there is a couple of comments that explain changes):

                                     

                                     

                                    import flash.events.Event;
                                    import flash.net.URLLoader;
                                    import flash.net.URLRequest;
                                     
                                    var xmlFileToSearch:XML;
                                     
                                    var loader:URLLoader = new URLLoader();
                                    var request:URLRequest = new URLRequest("xmlFileToSearch.xml");
                                    // always add listernes BEFORE you call load
                                    loader.addEventListener(Event.COMPLETE, onComplete);
                                    loader.load(request);
                                     
                                    function onComplete(event:Event):void
                                    {
                                         // there is no need for another loader instance - you definitely have one already
                                         xmlFileToSearch = new XML(loader.data);
                                         trace(xmlFileToSearch.toXMLString());
                                    }
                                     
                                    
                                    
                                    searchInput.text = "enter the receipt number";
                                    searchInput.restrict = "0-9";
                                    searchDisplay.text = "Search Results";
                                    searchButton.visible = true;
                                    searchButton.addEventListener(MouseEvent.MOUSE_DOWN, searchXMLFile);
                                     
                                    function searchXMLFile(e:MouseEvent):void
                                    {
                                         searchDisplay.text = "false";
                                         statusDisplay.text = "";
                                         // get node which <number> node value is the serach value
                                         var node:XMLList = xmlFileToSearch.receipt.(number == searchInput.text);
                                         // presence of number indicates that result is found
                                         if (Boolean(int(node.number)))
                                         {
                                              searchDisplay.text = node.model.text() + node.imei.text() + node.number.text();
                                              // cast to integer - node text is String
                                              switch (int(node.status))
                                              {
                                                   case 78: 
                                                        statusDisplay.text = "ready";
                                                   break;
                                       
                                                   case 9: 
                                                        statusDisplay.text = "return";
                                                   break;
                                       
                                                   default: 
                                                        statusDisplay.text = "underway";
                                                   break;
                                              }
                                         }
                                    }
                                    
                                    
                                    • 16. Re: Please, give me an advise how to make a search of a number in xml-file (as3.0)
                                      createflash@mail.ru Community Member

                                      Thank you very much for these useful and understandable advices! Thank you again for attention to my problem!

                                      • 17. Re: Please, give me an advise how to make a search of a number in xml-file (as3.0)
                                        createflash@mail.ru Community Member

                                        Hello!

                                         

                                        I'm sorry, i feel myself so confused and stupid, but my client refuse to work with xml-file, he want to work only with txt-file. And he angry and insisted want to get this function with using file-txt. Is it possible?

                                         

                                         

                                        Thank you, and so sorry again.

                                        • 18. Re: Please, give me an advise how to make a search of a number in xml-file (as3.0)
                                          Ned Murphy CommunityMVP

                                          What is the reason for not wanting an xml file?  An xml file can easily be named anything you like, meaning you can name it fileData.txt and still treat it as an xml file.  Is it the type of file that scares your client or is it the way they want to present the data?

                                          • 19. Re: Please, give me an advise how to make a search of a number in xml-file (as3.0)
                                            createflash@mail.ru Community Member

                                            As i know my client scares so that earlier data was read in txt and formed every 30 minutes by his computer programm and now he is concerned that his programm can't generate file XML. And so function will not work.

                                            • 20. Re: Please, give me an advise how to make a search of a number in xml-file (as3.0)
                                              Ned Murphy CommunityMVP

                                              It should be possible to use a txt file of some other format, though whatever that format is will determine how much work you need to do to try to parse the data from it.  If the text file is written in the form a variable=value pairs then you might be able to use AS3 features to process it.  Otherwise, you might have to create a special parsing routine.

                                              • 22. Re: Please, give me an advise how to make a search of a number in xml-file (as3.0)
                                                Ned Murphy CommunityMVP

                                                You're welcome.  If you find you need more help, you should start a new posting.  It helps to keep different problems separated into different postings.