• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 394
  • Last Modified:

How do I modify the first item in an xmlList?

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

0
worked
Asked:
worked
  • 2
  • 2
1 Solution
 
workedAuthor 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

0
 
DreammonkeyCommented:
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

0
 
workedAuthor 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

0
 
DreammonkeyCommented:
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

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now