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

Is it possible to search for keywords in an XML file?

New Here ,
Feb 01, 2010 Feb 01, 2010

Hey all,

I'm currently searching for ways to count the number of pre-defined keywords in an XML file. An example would be trying to find how many times "war" is mentioned in a news networks RSS feed <title></title> and adding it to a variable.

I've found a few custom AS3 XML class files out there but nothing I've been able to adapt. I'm at the point of considering just taking the title of these news stories converting them all to strings and searching them that way...

I'd be grateful for any help

Thanks!

TOPICS
ActionScript
2.2K
Translate
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

Community Beginner , Feb 02, 2010 Feb 02, 2010

Just to clarify JCSurf

In your code example, you're checking to see if the XML element has a attribute "NAMED" keywords.

If you are trying to determine the number of times the keyword "war"  appears in a specific XML newsfeed, I'm assuming that the XML elements already have an attribute named "keywords" and that you are checking for this attribute existing and then checking to see if the word "war exist within the keywords attribute and then finally counting the number of times that this is found.

...
Translate
Community Expert ,
Feb 01, 2010 Feb 01, 2010

unless you're searching for xml tags named "war", how else could you do this other than using string methods?

Translate
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
New Here ,
Feb 01, 2010 Feb 01, 2010

Sorry my as3 is still developing. What would the syntax look like searching a normal string value?

I'm looking for something along the lines of...

if myString contains "war" then myNum ++;

Cheers

Translate
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 Expert ,
Feb 01, 2010 Feb 01, 2010

you can use the numberOf() function below.  no change need be made to numberOf().  to call use:

var myNum:Number = numberOf(myString," war ");

//-----------------------------------------------------

function numberOf(s:String,key:String):Number{

return s.split(key).length-1;

}

Translate
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 ,
Feb 01, 2010 Feb 01, 2010

Couldn't you do a loop to search for the keyword "war" and each time a match is found a counter variable is increased by 1?

Assuming you have already set up your .fla file to load an external .xml file, created a variable named warCounter that is of the type "String"

ie:

//------------------------------

var warKeywordCounter:Number = 0;

var totalNewsItems:Number = newsFeed.items. newsItem.length();

// determine number of XML elements named "newsItem" within "items" node of the .xml document

for (var q:Number = 0; q < totalNewsItems; q++) {

     if (newsFeed.items.newsItem.attribute("keyword") == "war" {

          warKeywordCounter == warKeywordCounter+1;

          trace("Current value of warKeywordCounter = "+ warKeywordCounter)

     }

}

//------------------------------

Translate
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
New Here ,
Feb 01, 2010 Feb 01, 2010

Hey dchaparro,

Your solutions looks great to me but I can't figure out why your using "keyword" after the .attribute then comparing it against the keyword I actually want to use..."war"

I've been experimenting with something like this

// ---------------------------

function negativeKeyword(keywords:String)

{

     for (var i:Number = 0; i < 29; i++)

     {   

          if ( myXML.channel.item.title.attribute == keywords )

          {

               negativeNumber --;

          }

     }

}

Thanks!

Translate
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 ,
Feb 01, 2010 Feb 01, 2010

An XML element can have multiple attributes such as:

<title keyword="war" category="international">War Breaks Out in the Middle East!</title>

In the above example, the XML element <title> has 2 attributes named keyword and category.

So in my example I'm searching for the attribute named "keywords" within the element <newsItem>.

The XML document could be formatted something as the following:

============

<someNewsFeed>

     <items>

          <newsItem keyword="war" category="international" url="www.cnn.com/headline2010020111">War Breaks Out in the Middle East!</newsItem>

          <newsItem keyword="healthcare" category="politics" url="www.cnn.com/headline2010020321">Healthcare Bill Still Has Some Hurdles to Jump</newsItem>

          <newsItem keyword="lady gaga" category="entertainment" url="www.cnn.com/headline2010020981">Lady Gaga Has 360 Reach</newsItem>

     </items>

</someNewsFeed>

===========

So in my example, the if/else loop is looking through each XML node named <newsItem> and when it finds it, it then see if it has an attribute called "keyword" and if it does, it then looks to see if it contains the word "war". If it DOES, then it will do the proceeding actions.

===========

if (newsFeed.items.newsItem.attribute("keyword") == "war") {

          warKeywordCounter == warKeywordCounter+1; //increase your keyword counter

          trace("Current value of warKeywordCounter = "+ warKeywordCounter);

}

==========

I'm assuming you already know how to load external XML documents into flash.

I hope this helps.

Message was edited by: dchaparro Corrected if/else code to include right parentheses.

Translate
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 ,
Feb 02, 2010 Feb 02, 2010

Just to clarify JCSurf

In your code example, you're checking to see if the XML element has a attribute "NAMED" keywords.

If you are trying to determine the number of times the keyword "war"  appears in a specific XML newsfeed, I'm assuming that the XML elements already have an attribute named "keywords" and that you are checking for this attribute existing and then checking to see if the word "war exist within the keywords attribute and then finally counting the number of times that this is found.

============

function negativeKeyword(keywords:String)

{

     for (var i:Number = 0; i < 29; i++)

     {  

          if ( myXML.channel.item.title.attribute == keywords ) // this is only checking to see if an attribute exists that is named keywords

          {

               negativeNumber --;

          }

     }

}

============

this would check to see if the attribute keyword exists element tag and if it contains the word "war" .

if ( myXML.channel.item.title.attribute("keywords") == "war" )

I also believe that you need to have your strings in quotes.

Keep me posted.

Translate
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
New Here ,
Feb 02, 2010 Feb 02, 2010

Wow thanks for the knowledge chaparro!

The XML I'm working with just has a normal <title>News Story</title> format that I'm using as a string to search. My solution isn't really that elegant but gets the job done. I simply used the 'indexOf' string command to find out if the keyword I specified was in the string. It's kind of a hacky way as when its false it returns '-1' and when its true it returns a positive integer but hey it works

Many Thanks

Translate
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 ,
Feb 02, 2010 Feb 02, 2010
LATEST

LOL.

My apologies if I provided more information than what was required. I guess I figured that the XML elements were more complex than what you provided.

But hey, now you know a different way to search and grab information from an XML file.

Best of luck and keep on pushing through.

Translate
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