1

I'm trying to take text from an XML file (this text will be translated later so it must be held externally) and import it into separate dynamic text boxes in Flash (AS2). The text boxes are organised first by row (eg r1) then by page (eg p1) and finally by text line (eg l1), so to change the text in the first row, first page, first line you'd enter r1.p1.l1.text = "sometext".

This is my XML file. The first thing I need to do is to get the id from each <txt> tag and save it as a variable that I can use as the text box identifier but I don't know how!

<?xml version="1.0" encoding="utf-8"?>
<txt id="r1.p1.l1">First line of text</txt>
<txt id="r1.p1.l2">Second line of text</txt>

The XML is called in with the following function and the text boxes should be populated accordingly - the id has to be converted to an instance name; I've tried using a variable THE_XML_ID = this[someVar] but couldn't get that to work either...

function getData()
{
    var txt:XML = new XML();
    txt.ignoreWhite = true;
    txt.onLoad = function(success)
    {
        for (i = 0; i < txt.childNodes.length; i++){
            THE_XML_ID.text = txt.childNodes[i].childNodes[0];
        }
    };
    txt.load("assets/text/text.xml");
}

To sum up: I need to get the id from each <txt> tag in the XML file and use it as the text box identifier for that line of text - any help would be appreciated.

2
  • You shouldn't do txt.parseXML() on the loaded XML, it is for parsing a string, and is not needed when you load an XML document. help.adobe.com/en_US/AS2LCR/Flash_10.0/… Commented Apr 18, 2013 at 14:27
  • Thanks, I've removed it. Commented Apr 18, 2013 at 17:54

2 Answers 2

1

If you do the following in the onLoad method:

this['testText'].text = txt.childNodes[0].childNodes[0];

"this" refers to the XML object. Try with

_root['testText'].text = txt.childNodes[0].childNodes[0];

Anyway your approach won't work as you cant evaluate so deeper. As suggested in comments, it would be better to use another XML structure to make the targeting easier but if you really want to keep this logic, here is a working exemple :

// scope of your displayobjects, here they are on main timeline
var scope:MovieClip = _root; 
// get the id attributes and return the related displayobject
function getTargetFromId(id:String, scope:MovieClip):MovieClip {
    var target:MovieClip = scope;
    var path:Array = id.split('.');
    for (var i:Number=0;i<path.length;i++) {
       target = target[ path[i] ];
     }
     return target;
 }


function getData():Void{
  var txt:XML = new XML();
  txt.ignoreWhite = true;
  txt.onLoad = function(success) {
    for (i:Number = 0; i < txt.childNodes.length; i++){
       var id:String = txt.childNodes[i].attributes.id;
       getTargetFromId(id,scope).text = txt.childNodes[i].firstChild.toString();
    }
  }
  txt.load("assets/text/text.xml");
}
Sign up to request clarification or add additional context in comments.

Comments

1

For the basic question - instance name from a variable string - you would use the fact that any object property that can be accessed using dot notation, like myObject.someProperty can also be accessed using square brackets and a string, like myObject["someProperty"].

You can use that in cases with numbered identifiers, like the r1, p1 and l1 in your case. So for example if the variable i has the value 1 (i.e. in a foor-loop) r1["p"+i] would reference the the same thing as r1.p1. And assuming code is executed in the scope where r1 is present, you can use this, and do this["r"+i] to reference r1.

So in your case you would need to break apart the id="r1.p1.l2" from the XML and in place of THE_XML_ID use the above square brackets notation to reference the right text box. You could also restructure the XML, for easier access to the row, page and line numbers, for example by having something like <txt page="1" row="1" line="1">First line of text</txt>.

1 Comment

Thanks for that - but I'm still getting nowhere! If I create a new text box on the main timeline called testText and replace the for loop with testText.text = txt.childNodes[0].childNodes[0];, it works fine; but if I replace it with this['testText'].text = txt.childNodes[0].childNodes[0]; nothing happens. I'm obviously missing something here but don't know what...

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.