Hello! Sorry for troubling.
Please, give me an advice how to make a search of a number in xml-file (as3.0)
Thank you!
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.
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
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>
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")
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;
}
}
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
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;
}
}
}
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?
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.
North America
Europe, Middle East and Africa
Asia Pacific