Skip navigation
Currently Being Moderated

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

Aug 16, 2012 10:43 PM

Tags: #problem #flash #xml #action #ac3 #actionscript3

Hello! Sorry for troubling.

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

Thank you!

 
Replies
  • Currently Being Moderated
    Aug 17, 2012 4:54 AM   in reply to createflash@mail.ru

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

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 17, 2012 7:29 AM   in reply to createflash@mail.ru

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 18, 2012 5:07 AM   in reply to createflash@mail.ru

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 18, 2012 5:15 AM   in reply to createflash@mail.ru

    You're welcome

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 19, 2012 5:10 AM   in reply to createflash@mail.ru

    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")

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 19, 2012 7:56 AM   in reply to createflash@mail.ru

    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;
         }
    }
     
    
     
    |
    Mark as:
  • Currently Being Moderated
    Aug 19, 2012 9:02 AM   in reply to createflash@mail.ru

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 19, 2012 9:21 AM   in reply to createflash@mail.ru

    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;
              }
         }
    }
     
    
     
    |
    Mark as:
  • Currently Being Moderated
    Aug 27, 2012 5:26 AM   in reply to createflash@mail.ru

    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?

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 27, 2012 7:03 AM   in reply to createflash@mail.ru

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 27, 2012 7:54 AM   in reply to createflash@mail.ru

    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.

     
    |
    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