-
How to search each descendant of an XML object for a matching string
Suicide is not an option so i need your help...why won't this work?
I'm working on a search feature of my interface which lists devices. I want to be able to enter a term into a search field and then display only the ones who match. Caveat is that each device has close to 50 children. I need to parse through each device's children to check to see if anyone of them contains the string I am looking for. I can access it fine, but when I check for the string, it
Code:
for each(var device:XML in fileXML.*)
{
for each (var descendant:XML in device.*)
{
var field2check:String = descendant.name() + ": " + descendant.toString();
trace(field2check );
// problem is here, always evaluates to -1
Trace(field2check.lastIndexOf(searchStr));
}
}
---
Thinking outside of the box will get you fired if the "box" is strict budget.
-
I assume that high number of views and lack of responses means my question was ill formed...let's try this again...say I have this XML...
Code:
<devices>
<device>
<deviceID>1001</deviceID>
<city>Acme City</city>
<state>CA</state>
<zip>90210</zip>
</device>
<device>
<deviceID>1020</deviceID>
<city>Acme City</city>
<state>CA</state>
<zip>90210</zip>
</device>
<device>
<deviceID>1031</deviceID>
<city>Acme City</city>
<state>CA</state>
<zip>90210</zip>
</device>
</devices>
What I need to do is search this XML to see if a string that is typed into a search box (searchTxt) exists in any of the device nodes. IE, user types something in the search box, code checks each device node's children to see if any of the nodes contain the search string. So I need to check each node for the string.
Here's my code THAT DOES NOT WORK! Doesn't find matches when it should:
Code:
searchBtn.addEventListener(MouseEvent.CLICK, getSearchTerm);
searchBtn.buttonMode = true;
function getSearchTerm(evt:Event)
{
var searchStr:String = searchTxt.text;
trace ("search for > " + searchStr);
showSearchResults(searchStr);
}
function showSearchResults(str:String)
{
trace ("show Results called");
for (var i:int=0; i<fileXML.device.children().length(); i++)
{
trace(fileXML.device.children()[i] + " : " + fileXML.device.children()[i].toString().lastIndexOf(str));
}
}
HOWEVER, if I call the showSearchResults function explicitly with:
Code:
showSearchResults("20");
It works like a charm...I'm losing hair on this one please help
---
Thinking outside of the box will get you fired if the "box" is strict budget.
-
.
Hi,
Does this help you at all?
PHP Code:
var fileXML:XML;
var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest("file.xml"));
myLoader.addEventListener(Event.COMPLETE, processXML);
function processXML(e:Event):void
{
fileXML = new XML(e.target.data);
trace(fileXML);
}
searchBtn.addEventListener(MouseEvent.CLICK, getSearchTerm);
searchBtn.buttonMode = true;
function getSearchTerm(evt:Event)
{
var searchStr:String = searchTxt.text;
trace("search for > " + searchStr);
showSearchResults(searchStr);
}
function showSearchResults(str:String)
{
trace("show Results called");
for (var i:int=0; i<fileXML.device.children().length(); i++)
{
if (fileXML.device.children()[i].toString().indexOf(str) != -1)
{
trace(fileXML.device.children()[i] + " : " + str + " has been found");
}
//trace(fileXML.device.children()[i] + " : " + fileXML.device.children()[i].toString().lastIndexOf(str));
}
}
or to give its node name as well
PHP Code:
if (fileXML.device.children()[i].toString().indexOf(str) != -1)
{
trace(fileXML.device.children()[i].name() + " - " + fileXML.device.children()[i] + " : " + str + " has been found");
}
i needed to load the xml file too.
Last edited by fruitbeard; 01-03-2014 at 12:25 PM.
-
Same problem...no matching results...
for whatever reason fileXML.device.children()[i].toString().indexOf(str) != -1 returns false so the code if the if statement never runs.
Did it work on your machine?
---
Thinking outside of the box will get you fired if the "box" is strict budget.
-
.
Hi,
My output was
search for > 1020
show Results called
deviceID - 1020 : 1020 has been found
-
Weird......can you send me your FLA?
---
Thinking outside of the box will get you fired if the "box" is strict budget.
-
.
Hi,
Its not much, CS5, as low as I can go
-
Fruitbeard my man...thnx...it was some weird ish going on with the textfield... I deleted the searchTxt textbox and created another one and all of sudden it starts working!?? I copied the original in my file into your FLA, and it wouldn't work there either...strange....either way PROBLEM SOLVED!!!
---
Thinking outside of the box will get you fired if the "box" is strict budget.
-
.
Hi,
Glad I could help 4Sheezy, no idea what the textfield problem was without seeing it, perhaps you didn't embed it!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|