PDA

View Full Version : Displaying XML in textField...



GhandiJones
October 8th, 2007, 03:33 PM
Ok... I've been stumped over this for a little while, and just can't get it. I have done the Using XML in Flash CS3/AS3 tutorial, and have been unsuccessful at achieving all nodes of my xml to display in a textfield. My trace shows everything correctly, however, on the stage I only get one line. This is the code...

import flash.display.*;
import flash.text.*;

var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();

xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
xmlLoader.load(new URLRequest("../xml/news.xml"));

var myTF:TextField = new TextField();
myTF.multiline = true;
myTF.autoSize = "left";
myTF.border = true;
myTF.textColor = 0xFFFFFF;
myTF.wordWrap = true;

addChild(myTF);

function LoadXML(e:Event):void {
xmlData = new XML(e.target.data);
ParseNews(xmlData);
}

function ParseNews(NewsInput:XML):void {

var newsChildren:XMLList = NewsInput.Article.children();

for each (var newsInfo:XML in newsChildren) {
myTF.text = newsInfo;
trace(newsInfo);
}
}

And this is the XML...

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

<News>
<Article>
<Headline>News Article 1</Headline>
<Date>XX.XX.XX</Date>
<Content>Whatever you want</Content>
</Article>
<Article>
<Headline>News Article 2</Headline>
<Date>XX.XX.XX</Date>
<Content>Whatever you want</Content>
</Article>
</News>


If you guys have got any ideas... I just can not figure out what I am doing wrong. I'm trying, but I just can't get it. Thanks.

zubin101
October 8th, 2007, 03:46 PM
Whats happening is that your For Loop is replacing each line instead of adding to it. I'd create a temp string variable to store those lines outside of that loop:

function ParseNews(NewsInput:XML):void {
var newsChildren:XMLList = NewsInput.Article.children();
var xmlString:String = "";
for each (var newsInfo:XML in newsChildren) {
xmlString = xmlString + newsInfo;
}
myTF.text = xmlString;
trace(newsInfo);

}

GhandiJones
October 8th, 2007, 03:55 PM
Ok that got everything to display, but the formatting I was trying to maintain from my trace was lost. I want to be able to eventually use CSS to display all of this information, so I need them segregated.