silvith
silvith

Reputation: 290

AS3 Flash CC XML need to find the attribute of a node, node number unknown

I have imported an xml-file and now I'm running into a problem that makes me feel really stupid :/

the xml-structure:

<xml>
 <mylist>
  <category cat=klank>
   <word aw=nk>Word</word>
   (there are 12 of these word nodes)
  </category>
 </mylist>
</xml>

In flash I have a var called curWord which is a randomly determined word from my category. I don't know which node-number it is in my xml.

I have a variable string called curAw. It needs to contain the aw-attribute of curWord.

Then I did:

curAw = list.category.(@cat == klank).(word == curWord).@aw

But it doesn't work that way. And I'm not sure what will. I've spent a good hour trying stuff and searching the web, but I'm not sure how to describe what I need to know, so I can't find anything.

As always, your help is much appreciated :)

Upvotes: 0

Views: 488

Answers (2)

null.point3r
null.point3r

Reputation: 1103

Without looping:

list.mylist.category.(@cat == "klank").word.(text()==curWord).@aw

Upvotes: 1

net.uk.sweet
net.uk.sweet

Reputation: 12431

A couple of things. I think you need to quote the values of your attributes (at least I get an error in Flash CS5 if I don't). Keep an eye on the console for errors to catch this sort of thing. Also, myList is part of the node hierarchy so you need to reference the category node via that.

You may be able to do what you want without looping, but I couldn't see a way in the documentation. However, you can definitely get the value you need by looping over the word nodes in the selected category like this:

var list:XML = new XML('<xml><mylist><category cat="klank"><word aw="nk">Word</word><word aw="ok">Word2</word><word aw="pk">Word3</word></category></mylist></xml>');
var curWord = 'Word';

var words:XMLList = list.mylist.category.(@cat == 'klank').word; 

// Loop over the word nodes from the selected category
for each(var word:XML in words) 
{
        // Find the node with value matching curWord
    if (word.text() == curWord) {
        trace(word.@aw);
    }
}

Upvotes: 2

Related Questions