• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Community Beginner ,
Aug 16, 2012 Aug 16, 2012

Copy link to clipboard

Copied

Hello! Sorry for troubling.

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

Thank you!

TOPICS
ActionScript

Views

3.3K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Aug 18, 2012 Aug 18, 2012

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

...

Votes

Translate

Translate
LEGEND ,
Aug 17, 2012 Aug 17, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 17, 2012 Aug 17, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 17, 2012 Aug 17, 2012

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 17, 2012 Aug 17, 2012

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 18, 2012 Aug 18, 2012

Copy link to clipboard

Copied

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;

                    var textList:XMLList = item.text();

                    xmlCount++;

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

                    {

                              var t:XML = textList;

                              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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 18, 2012 Aug 18, 2012

Copy link to clipboard

Copied

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 == 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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 18, 2012 Aug 18, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 18, 2012 Aug 18, 2012

Copy link to clipboard

Copied

You're welcome

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 18, 2012 Aug 18, 2012

Copy link to clipboard

Copied

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.model.toString();

                    var imei:XMLList = pageList.imei.toString();

                    var number:XMLList = pageList.number.toString();

                    if (pageList == 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.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>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 19, 2012 Aug 19, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 19, 2012 Aug 19, 2012

Copy link to clipboard

Copied

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;

     }

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 19, 2012 Aug 19, 2012

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 19, 2012 Aug 19, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 19, 2012 Aug 19, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 19, 2012 Aug 19, 2012

Copy link to clipboard

Copied

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;
          }
     }

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 19, 2012 Aug 19, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 26, 2012 Aug 26, 2012

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 27, 2012 Aug 27, 2012

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 27, 2012 Aug 27, 2012

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 27, 2012 Aug 27, 2012

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 27, 2012 Aug 27, 2012

Copy link to clipboard

Copied

Thank you for explain and help.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 27, 2012 Aug 27, 2012

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines