0

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 :)

5
  • Your root node is mylist but you reference it via list (list.category...). Is that a typo? Commented Sep 6, 2013 at 12:36
  • @net.uk.sweet Not a typo. I imported the xml as var list:XML = (load xml and stuff data in var) Commented Sep 6, 2013 at 12:39
  • How are you getting the randomly determined word from your category? Can't you get the attribute when you retrieve that? Commented Sep 6, 2013 at 12:41
  • @net.uk.sweet if I could, I would. I have an array with (one to three) words coming over from a different class and I fill it's total up to (3 -12) in this class randomly. It's a bit complicated. Commented Sep 6, 2013 at 12:52
  • It always is complicated ;) Commented Sep 6, 2013 at 13:08

2 Answers 2

2

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);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Don't worry about my code: I often simplify a long piece if I have to ask a question here. But I realise that can be quite annoying for people who need all possible information to help solve a problem.
Anyway, the piece I was looking for but couldn't find bloody anywhere was woord.text(). You have helped me immensely and possibly made my weekend, thank you :)
1

Without looping:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.