We help IT Professionals succeed at work.

How do I modify the first item in an xmlList?

worked
worked asked
on
Medium Priority
400 Views
Last Modified: 2013-11-11
Hey there-

I have a very simple for loop that parses an xml doc into textfields.  Each textfield is added to a container within the loop for animation purposes later.  Question.  How do I modify the first item in the xmlList so that it displays an image instead of text?  My code is below.  Thanks! Any help is appreciated.
ACTIONSCRIPT 3:
for (var i:int = 0; i < xmlList.length(); i++) {
	var container:MovieClip = new MovieClip();
	addChild(container);
	
	var tf:TextField = new TextField();
	tf.htmlText = xmlList[i].title;
	//not working - tf.htmlText = '<img src="'+ xmlList[0].title +'">';
	tf.autoSize = TextFieldAutoSize.CENTER;
	tf.x = startPos;
	tf.y = (stage.stageHeight / 2) - (tf.height / 2);
	widthValue = tf.width;
 
	container.path = xmlList[i].path;
	container.addChild(tf);
	container.addEventListener(MouseEvent.CLICK, onContainerDown);
	startPos = startPos + (widthValue + spacer);
}
 
XML:
<?xml version='1.0' encoding='ISO-8859-1' ?>
<bc>
<item>
<title>image.gif</title>
<path>/</path>
</item>
<item>
<title>000000</title>
<path>/bc</path>
</item>
<item>
<title>000000</title>
<path>/bc/bc_1</path>
</item>
</bc>

Open in new window

Comment
Watch Question

Author

Commented:
Also, if it is easier to create the graphic in actionscript and replace the first item in the xmlList, then by all means please show me that method as well or instead.  My code below attempts this but displays [object Sprite] in the textfield instead of the graphic.  Any help is appreciated!  Thanks!
ACTIONSCRIPT:
var icon:Sprite = new Sprite();
icon.graphics.lineStyle(1, 0x000000);
icon.graphics.moveTo(0, 0);
icon.graphics.lineTo(-6, (-stage.stageHeight/2 + 2));
icon.graphics.moveTo(0, 0);
icon.graphics.lineTo(-6, stage.stageHeight/2 - 2);
icon.x = startPos - (spacer / 2);
icon.y = stage.stageHeight / 2;
for (var i:int = 0; i < xmlList.length(); i++) {
        var container:MovieClip = new MovieClip();
        addChild(container);
        
        var tf:TextField = new TextField();
        tf.htmlText = xmlList[i].title;
        // not working - tf.htmlText = '<img src="'+ xmlList[0].title +'">';
        // replace first item in list
        // xmlList[0].title = hi;  // this outputs [object Sprite] instead of the graphic.
        tf.autoSize = TextFieldAutoSize.CENTER;
        tf.x = startPos;
        tf.y = (stage.stageHeight / 2) - (tf.height / 2);
        widthValue = tf.width;
 
        container.path = xmlList[i].path;
        container.addChild(tf);
        container.addEventListener(MouseEvent.CLICK, onContainerDown);
        startPos = startPos + (widthValue + spacer);
}

Open in new window

try this:

You can escape the ' and " characters by putting a \ before them...

DM
tf.htmlText = "<img src=\""+ xmlList[0].title +"\" >";

Open in new window

Author

Commented:
I had to place a dynamic textfield on the stage and load the graphic into that.  I also used a nested for loop to display and position this graphic among the other textfields.  

Question.  Why can't I load a graphic into a textfield that is dynamically created, yet can load one into a dynamic textfield drawn on the stage?  

For instance, where textList[i].link = image.gif


This WON'T load a graphic into a textfield:
 
var img:TextField;
for (var i:uint = 0; i < textList.length(); i++) {
	img = new TextField();
	img.htmlText = "<a href=\"" + textList[i].link + "\"><img src=\"" + textList[i].title + "\"></a>";
	img.x = 0;
	img.y = 0;
        addChild(img);
}
 
 
However, this WILL load the graphic:
 
for (var i:uint = 0; i < textList.length(); i++) {
	//load graphic into dynamic textfield on stage... named img
	img.htmlText = "<a href=\"" + textList[i].link + "\"><img src=\"" + textList[i].title + "\"></a>";
	img.x = 0;
	img.y = 0;
}

Open in new window

I knows it sounds crazy but I found that adding a space in front of the string could solve things...


NOT:
img.htmlText = "<a href=\"" + textList[i].link + "\"><img src=\"" + textList[i].title + "\"></a>";
 
BUT:
img.htmlText = " <a href=\"" + textList[i].link + "\"><img src=\"" + textList[i].title + "\"></a>";

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.