Skip to main content
Known Participant
February 27, 2010
Answered

xml search

  • February 27, 2010
  • 2 replies
  • 383 views

this is an attempt, i need some guidance/advice how i should search for excact word in text/setences in the xml

I put the whole xml's loaded as a string into an array

stringArray.push(testXML); //trace(stringArray)

then i split it and loop it to check if it matches the keyword.

and if is true i should get the trace... but i am not getting the result i want, when i know when certain word in a sentence should be TRUE,

i aint get the trace : /

for (var s:int = 0; s < stringArray.length; s++) {

     var condition:Boolean = new Boolean();

     var searchArray:Array = stringArray.split(" "); //trace(searchArray);

          for (var r:int = 0; r < searchArray.length; r++) {

               if (searchArray == keyword ) {

               trace("true");

          }

     } // end for r

}

any suggestions?

This topic has been closed for replies.
Correct answer Andrei1-bKoviI

Depending on the complexity of the search there are several approaches that can be used. If you want a more sophisticated way - you, perhaps, are better off with regular expressions. If your task is to find that a particular word or phrase exist - simple String methods can be used.

But, first of all, in order to use String functions you need to cast your XML to String - XML instance doesn't have String's capabilities including split() - this is, perhaps, why you don't get the trace.

Here is an example that illustrates how to find if a single instance of keyword exists in xml. The idea is to cast xml to String and find an index of first keyword occurance.

var myXML:XML = <data>
     <node>Dogs talk bark</node>
     <node>Cats talk meow</node>
     <node>Snakes talk hiss</node>
     <node>People talk speak</node>
</data>;

var keyword:String = "People";
trace(String(myXML).indexOf(keyword)); // traces 107
               
keyword = "Dogs talk";
trace(String(myXML).indexOf(keyword)); // traces 15
               
keyword = "Birds talk";
trace(String(myXML).indexOf(keyword)); // traces -1 - no such thing

2 replies

Andrei1-bKoviICorrect answer
Inspiring
February 28, 2010

Depending on the complexity of the search there are several approaches that can be used. If you want a more sophisticated way - you, perhaps, are better off with regular expressions. If your task is to find that a particular word or phrase exist - simple String methods can be used.

But, first of all, in order to use String functions you need to cast your XML to String - XML instance doesn't have String's capabilities including split() - this is, perhaps, why you don't get the trace.

Here is an example that illustrates how to find if a single instance of keyword exists in xml. The idea is to cast xml to String and find an index of first keyword occurance.

var myXML:XML = <data>
     <node>Dogs talk bark</node>
     <node>Cats talk meow</node>
     <node>Snakes talk hiss</node>
     <node>People talk speak</node>
</data>;

var keyword:String = "People";
trace(String(myXML).indexOf(keyword)); // traces 107
               
keyword = "Dogs talk";
trace(String(myXML).indexOf(keyword)); // traces 15
               
keyword = "Birds talk";
trace(String(myXML).indexOf(keyword)); // traces -1 - no such thing

Known Participant
February 28, 2010

Thank you all for advice. Got it to work now.

kglad
Community Expert
Community Expert
February 27, 2010

you can use:

function findstringF(s:String,keyword:String):Boolean{

if(s.split(word).length>1){

return true;

} else {

return false;

}

}