PDA

View Full Version : How to load text from xml to dynamic text box


dirtysnipe
03-01-2012, 01:57 PM
Im trying to load text from a node within an xml file to display in a dynamic text box.

How can I do this?

XML
<?xml version="1.0" encoding="utf-8"?>
<menu>
<student>
<name>name in here</name>
</student>
<care>
<url>file:\\10.0.0.5\care\</url>
</care>
<education>
<url>file:\\10.0.0.5\education\</url>
</education>
<psych>
<url>file:\\10.0.0.5\psychology\</url>
</psych>
</menu>

AS3

//Load External XML File

var myXML:XML;
var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest("links.xml"));
myLoader.addEventListener(Event.COMPLETE, processXML);

function processXML(e:Event):void {
myXML = new XML(e.target.data);

trace(myXML.student.name);
}


The instance name for the dynamic text box is :- stuname

Thanks in advance.

adaminaudio
03-01-2012, 02:13 PM
Hi there,

your code seems right. So if I'm understanding you correctly, all you have to do is this in your complete handler (or anywhere after the xml has loaded)

function processXML(e:Event):void {
myXML = new XML(e.target.data);

trace(myXML.student.name);

//into dynamic text here...
stuname.text = myXML.student.name;

}

dirtysnipe
03-01-2012, 02:35 PM
odd.

I get the output of the name but it does not display in the textbox.

Xegnma
03-01-2012, 02:48 PM
When you write:
myXML.student.name

What you are actually generating is a series of XMLLists. The quickest solution is to index the first element in the list like so:

myXML.student.name[0]

I would suggest reading the 'Actionscript 3.0 Developer's Guide', specifically the section titled 'Working with XML'. There are a bunch of things you may need to know to work with native XML effectively.

henke37
03-01-2012, 03:19 PM
Actually, an XMLList will decay into the XML element if it has only one XML element.

FlexFlashApps
03-01-2012, 03:24 PM
var myXML:XMLList;
var myLoader:URLLoader = new URLLoader();
function processXML(e:Event):void {
//myXML = new XML(e.target.data);
myXML = new XML(e.target.data).children();
for (var val in myXML) {
trace(myXML.children()[val]+"\n-------------");
txt.text+=(myXML.children()[val])+"\n";
}

}
myLoader.addEventListener(Event.COMPLETE, processXML);
myLoader.load(new URLRequest("links.xml"));



FFA

dirtysnipe
03-01-2012, 03:43 PM
All i get is from flah output


name in here
-------------
file:\\10.0.0.5\care\
-------------
file:\\10.0.0.5\education\
-------------
file:\\10.0.0.5\psychology\
-------------


no output to dynamic text box.


event changed instance of box to txt and that didnt fix it either.

FlexFlashApps
03-01-2012, 04:18 PM
Is your box a TextField? or a MovieClip?

There is no other scope to help you further unless you post your FLA in lower version of CS5.


FFA

dirtysnipe
03-02-2012, 03:34 PM
Very odd this.

I changed the text from classic text to TLF text and it now displays..

Thanks alot guys



Im still trying to solve a few issues with buttons and an image but Ill add a clean thread and upload the fla file.