views:

185

answers:

1

I am building a mini language translator out of XML nodes and Actionscript 3. I have my XML ready. It contains a series of nodes with two children in them. Here is a sample:

<translations>
 <entry>
 <english>man</english>
 <cockney>geeza</cockney>
 </entry>
 <entry>
 <english>woman</english>
 <cockney>lily</cockney>
 </entry>
</translations>

The AS3 part consist of one input box named "textfield_txt" where the English word will be typed in. An output text field for the translation called "cockney_txt". Finally, one button to execute the translation called "generate_mc".

The idea is to have actionscript look through the XML for key English words after the user types it in the "textfield", cross-freferences the children then returns the Cockney translation as a value.

The trouble is, when I test it I get no response or error messages- it's completely silent. I'm not sure what I'm doing wrong.

At present, I have setup a conditional statement to tell me whether the function works or not. The result is, no it's not! Here's the code below. I hope someone can help.

Cheers!

generate_mc.buttonMode=true;

var English:String;
var myXML:XML;
var myLoader:URLLoader = new URLLoader();

myLoader.load(new URLRequest("Language.xml"));
myLoader.addEventListener(Event.COMPLETE, processXML);

function processXML(e:Event):void {
myXML = new XML(e.target.data);
}


generate_mc.addEventListener(MouseEvent.CLICK, onClick);

function onClick(event:MouseEvent) {
 English = textfield.text;
 cockney_txt.text = myXML.translations.entry.cockney;

if(textfield.text.toLowerCase() == myXML.translations.entry.english.toLowerCase){ //return myXML.translations.entry.cockney; trace("success"); }else{ trace("try again!"); // ***I get this as a result }

}

A: 

What I would do in this situation is create an object (key value pairs) containing the English and corresponding cockney "translations." To do so...

var langObj:Object = new Object();
for (var entry:XML in myXML.children()) {
    langObj[entry.english.toString()] = entry.cockney.toString();
}

Now you can just reference an English entry in the langObj Object to get the corresponding cockney.

So, you would do something like:

if (langObj[textfield.text] != undefined) {
    trace( langObj[textfield.text] );
} else {
    trace( "the key is not defined: " + textfield.text);
}

EDIT

for each (var entry:XML in myXML.children()) {
    langObj[entry.english.toString()] = entry.cockney.toString();
}

for each (var ...

sberry2A
Thanks for the assistance, sberry2A.I did get an error when I applied your script:1067: Implicit coercion of a value of type String to an unrelated type XML.It's referencing this particular line:for (var entry:XML in myXML.children()) {What should I do?
Dwayne
See my edit. Never tested the code, and should have mentioned that. The **each** was missing in my for ... in loop.
sberry2A