PDA

View Full Version : XML newbie


bbonnet18
04-24-2005, 01:09 AM
I just started working with XML. I understand the idea of creating objects, but I can't seem to display any of my information in flash once I load it. I need to know how to get the values from the XML object to the interface so the user can see it. I can't even display the values with the trace command. My code for the XML and flash are below, please help me out.

XML Code from file- thisFile.XML:

<MyValues>
<Name>Joe Johnson</Name>
<Name>Susie Sanchez</Name>
<MyValues>



Flash Code that loads the above XML file into the flash movie:

myXML = new XML();
myXML.load("thisFile.XML");


_root.myVar = myXML.firstChild;


I really need to know how to get to the actual name "Joe Johnson" from my first child node and display that on the screen. Please let me know what I'm doing wrong.

mrand01
04-24-2005, 04:12 AM
The first problem is your example file is not well-formed XML.

<MyValues>
<Name>Joe Johnson</Name>
<Name>Susie Sanchez</Name>
</MyValues> <--needs a slash before MyValues to close the node

From there, this is the way to get the name Joe Johnson into a variable:

[as]
var myXML:XML = new XML();

myXML.onLoad = function(bSuccess:Boolean) {
if (bSuccess) {
var strJoeJohnson:String = myXML.firstChild.firstChild.firstChild;

trace (strJoeJohnson); //will trace out "Joe Johnson"
}
}

myXML.load("thisFile.XML");

bbonnet18
04-24-2005, 07:58 PM
I tried the above code, and this is the error I get:

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 5: Type mismatch in assignment statement: found XMLNode where String is required.
var strJoeJohnson:String = myXML.firstChild.firstChild.firstChild;

mrand01
04-25-2005, 06:54 PM
uh, so change it to an XMLNode then, or just cast the XMLNode as a string...