Skip to main content
January 6, 2012
Answered

converting XML into random String for text fields

  • January 6, 2012
  • 1 reply
  • 870 views

Hi,

I am now trying tolearn XML as a way to solve a word game project and I can see why it was suggested as a good direction to go in.

Currently I have sketched out this small code just to see how it might work.

Each text box receives a seperate word one is the game word/the other is a hint.

I was hoping to have a new one appear each time I tested the Movie but I cannot seem to get it.

Any suggestions to get me going in the right direction?

Thanks

import flash.net.URLLoader;

import flash.events.Event;

var allWords:Array;

var thisWord:String;

var gameWordsXML:XML;

var xmlLoader:URLLoader = new URLLoader();

xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);

xmlLoader.load(new URLRequest("words.xml"));

function xmlLoaded(event:Event):void

{

    gameWordsXML = new XML(event.target.data);

    /*gameWordsXML:String = event.target.data;

    allWords = gameWordsXML.split(",");*/

    gameWordsXML[Math.floor(Math.random() * gameWordsXML.length())];

    for (var i:int; i < gameWordsXML.length(); i++)

    {

        text_boxA.text = gameWordsXML.word. @ val;

        text_boxB.text = gameWordsXML.word. @ hint;

       

    }

}

This topic has been closed for replies.
Correct answer _spoboyle

using

<words>

    <word val="car" hint="blue" />

    <word val="house" hint="roof" />

    <word val="cloud" hint="rain" />

    <word val="beach" hint="sand" />

    <word val="man" hint="girl" />

    <word val="sound" hint="music" />

</words>

use

private function xmlLoaded(event:Event):void

{

    gameWordsXML = new XML(event.target.data);

   

    var words:XMLList = gameWordsXML.elements("word");

    var randomIndex:int = Math.random() * words.length();

   

    text_boxA.text = gameWordsXML.word[randomIndex].@ val;

    text_boxB.text = gameWordsXML.word[randomIndex].@ hint;

}

the length() method of an XML object always returns 1

you must get the XMLList of the nodes you are interested in and then call length() on that

1 reply

Ned Murphy
Legend
January 6, 2012

Don't use a for loop, that will end up only displaying the last elements all of the time.  Instead, calculate a random value for i from the gameWordsXML.length and use that...

function xmlLoaded(event:Event):void

{

    gameWordsXML = new XML(event.target.data);

    /*gameWordsXML:String = event.target.data;

    allWords = gameWordsXML.split(",");*/

   

      var i:uint = Math.floor(Math.random()*gameWordsXML.length());

        text_boxA.text = gameWordsXML.word. @ val;

        text_boxB.text = gameWordsXML.word. @ hint;

}

I am not sure about your parsing, but if you were getting vakues in the textfields with what you had, the above changes should work.

January 6, 2012

Hi,

Yes that makes sense I get it!.

However, I'm not generating anything random. It gets stuck on the two first words val/hint.

I looked up correct parsing and, based on that reading, came up with these two versions:

<?xml version="1.0" encoding="UTF-8"?>

this is my first try(I would prefer this opition)

<words>

    <word val="car" hint="blue" />

    <word val="house" hint="roof" />

    <word val="cloud" hint="rain" />

    <word val="beach" hint="sand" />

   

    <word val="man" hint="girl" />

   

    <word val="sound" hint="music" />

</words>

this is the new code:

import flash.net.URLLoader;

import flash.events.Event;

var allWords:Array;

var thisWord:String;

var gameWordsXML:XML;

var xmlLoader:URLLoader = new URLLoader();

xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);

xmlLoader.load(new URLRequest("words.xml"));

function xmlLoaded(event:Event):void

{

gameWordsXML = new XML(event.target.data);

gameWordsXML:String = event.target.data;

allWords = gameWordsXML.split(",");

gameWordsXML[Math.floor(Math.random() * gameWordsXML.length())];

for (var i:int; i < gameWordsXML.length(); i++)

var i:uint = Math.floor(Math.random() * gameWordsXML.length());

text_boxA.text = gameWordsXML.word. @ val;

text_boxB.text = gameWordsXML.word. @ hint;

This is my recent try:

<?xml version="1.0" encoding="UTF-8"?>

<words>

    <word>

        <val>"car"</val>

        <hint>"blue"</hint>

        <val>"house"</val>

        <hint>"roof"</hint>

        <val>"beach"</val>

        <hint>"sand"</hint>

        <val>"man"</val>

        <hint>"girl"</hint>

        <val>"music"</val>

        <hint>"sound"</hint>

    </word>

</words>

Here is the code for this version:

function xmlLoaded(event:Event):void

{

    gameWordsXML = new XML(event.target.data);

    var i:uint = Math.floor(Math.random() * gameWordsXML.length());

    text_boxA.text = gameWordsXML.word.val;

    text_boxB.text = gameWordsXML.word.hint;

}

_spoboyle
_spoboyleCorrect answer
Inspiring
January 6, 2012

using

<words>

    <word val="car" hint="blue" />

    <word val="house" hint="roof" />

    <word val="cloud" hint="rain" />

    <word val="beach" hint="sand" />

    <word val="man" hint="girl" />

    <word val="sound" hint="music" />

</words>

use

private function xmlLoaded(event:Event):void

{

    gameWordsXML = new XML(event.target.data);

   

    var words:XMLList = gameWordsXML.elements("word");

    var randomIndex:int = Math.random() * words.length();

   

    text_boxA.text = gameWordsXML.word[randomIndex].@ val;

    text_boxB.text = gameWordsXML.word[randomIndex].@ hint;

}

the length() method of an XML object always returns 1

you must get the XMLList of the nodes you are interested in and then call length() on that