Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help Loading XML text dynamicly into Flash MX 1

Status
Not open for further replies.

theKeyper

Technical User
Jun 11, 2003
79
AU
Hi everyone.
I was wondering if any one could point me to a good tutorial. or maybe just give me some tips. I have a text file loading into a dynamic text box in MX. How do I do the same thing with an XML file? I have absolutley no idea how to use XML. I just want standard text, nothing Flashy. Any help is greatly appreciated. Thanks,
-Keyper
 
If you just want standard text then you're as well sticking with a text file using name/variable pairs. For XML there are two new things to consider:

1. The information you're bringing in has to be valid and well-formed XML - saved as a textfile with a .xml extension.

2. The information has to be loaded and parsed via the methods of the Flash XML Class.

XML files could look like this:

Code:
<?xml version='1.0'?>
<menu>
	<item title='Beefburger'>
		<price>4.95</price>
		<SKU>XS293483</SKU>
	</item>
		<item title='Turkey Burger'>
		<price>4.95</price>
		<SKU>XS293483</SKU>
	</item>
</menu>

Looks like HTML but has custom tags which you name yourself so that the data is easily read and understood.

The Flash used to read this can be complex but comes down to three main areas - the object used to hold the data, the code used to parse the data (get the information out into a useable format) and the loading process itself.

Code:
doXML = function () {
        //create an instance of the class to hold the data
	thisXML = new XML();
        //this sets Flash to ignore any whitespace in the file (empty lines used to make the XML more human readable)
	thisXML.ignoreWhite = true;
        //fires the code to parse the data when it's loaded (custom function)
	thisXML.onLoad = parseXML;
        //load the XML in
	thisXML.load('xml/gallery.xml');
};

The actual parsing code itself generally varies depending on what the format of the XML file is but the things to be aware of are nodes and attributes - the different places in which data can be stored.

Best tutorial I've seen on this stuff is from Colin Moock ( in his book ActionScript The Definitive Guide.
 
Thank you so much Wangbar for that help, however I feel I'm still only part of the way there. I've constructed a sample, just a dynamic text box which I want to have display the contents of an XML file. I agree that it would be better to use a .txt file, however for complex reasons that I will not elaborate on it needs to be XML. I am sure this has a fairly simple solution so if anyone could take a look at the file I would much appreciate it.
you may access the zip file here: It is made in Flash MX. Thank you so much for your time.
-Keyper.
 
This will work for your example - you hadn't called the doXML function or provided a parseXML function.

Code:
parseXML = function () {
	//break up the XML and pull out the data
	//childNodes[0] is the top level of the XML document, firstChild is the first node of the top level
	//the way you access nodes and values is very much dependent on how your XML is structured
	//so this example isn't a general approach it's specific to your XML document
	_root.mytextbox.text = thisXML.childNodes[0].firstChild;
};
doXML = function () {
	thisXML = new XML();
	thisXML.ignoreWhite = true;
	//this actually has to have a function associated with it
	thisXML.onLoad = parseXML;
	thisXML.load('Muffet.xml');
};
//you have to call the function in order to load the data 
doXML();
 
RIGHTEOUS!!!
Thank you so much mate! just one more question. How do I modify the code to access the second or third child node in the 'root level', so to speak, of the XML file. I have several .fla's and each need to access a different node of the XML file. here is an example. Obviously there is no secondChild, thirdChild, etc. so I tried nextSibling but there is nothing to tell it to get to the third node. and lastChild doesn't seem to work either. Thanks a lot for your time.
-Keyper
 
You're not actually creating a well-formed XML document in your example - you need to create a hierarchy for Flash to be able to deal with it effectively. Here's your text as a basic XML file:

Code:
<root>
	<text>
	Little Miss Muffet sat on her tuffet.
	</text>
	<text>
	Eating her Curds and Whey
	</text>
	<text>
	Along Came A Spider.
	</text>
	<text>
	And scared Miss Muffet away.
	</text>
</root>

Then to parse it there are several ways to approach the doc but the easiest is to make an array of the nodes and then step through it sequentially with a for loop:

Code:
parseXML = function () {
	//create an array of the XML nodes
	arrNodes = thisXML.firstChild.childNodes;
	for (var i = 0; i<arrNodes.length; i++) {
		//iterate through the array pulling out each node's text content
		textValue = arrNodes[i].firstChild;
		//here are the values
		trace(textValue);
		//here's an easy way to display them
		_root.mytextbox.text += textValue+"\n";
	}
};

THe whole XML/Flash thing is a big topic - check out the Moock book or some online tutorials for some proper depth on it.
 
Mate that all looks real cool but you kinda lost me with that 'for' statement. Do you reckon you could go over that line in depth? as well as the last line. what is the "\n" about? Sorry I'm so slow. and I will try to get the Moock book as soon as I have any money. Thanks a lot.
-Keyper
 
'\n' is the control code for 'newline' so it will format the text in the textbox with each phrase on its own line - like '<br>' if you were using an HTML formatted textbox.

Code:
arrNodes = thisXML.firstChild.childNodes;

sets up an array which contains the children of 'root' - i.e. all of the nodes named 'text'. The for loop uses the length of this array as a limit so that you can work through each node in turn. <root. is the first child node of the full document (thisXML.firstChild) which in turn contains its own children which we access through 'childNodes'.

Code:
 textValue = arrNodes[i].firstChild;

pulls out the data: each member of the array we've created arrNodes[ i ] would be the whole line <text>Little Miss Muffet sat on her tuffet.</text> which contains the child node 'little miss muffet...' so we access this through 'firstChild'

The whole document structure is basically a bunch of 'text' nodes which are children of the 'root' node - each of which in turn contains a child node which is the actual text you want to use in your movie.
 
Beautiful! I understand it all now. However what would I do if I only wanted the text box to display 'Along Came A Spider.' or the child node of the third 'text' node?

The Key is more powerful than the Sword.
 
Stick the values you read in into an array instead of writing them to the screen then you can call them up from anywhere in the movie just by using the array index:

Code:
 textValue[i] = arrNodes[i].firstChild;

then...

Code:
textbox.text=textValue[2]//etc...
 
Sorry over and over dude. What am I doing wrong? now I don't see anything? I don't suppose you could fix up my code and send it back to me? I think you understand that I want one sentance on each page? Thank you so, so much!
-Keyper.

The Key is more powerful than the Sword.
 
You need to create an array before you can fill it with stuff.

Here's how the parse function should look:

Code:
parseXML = function () {
	textValue = new Array();
	arrNodes = thisXML.firstChild.childNodes;
	for (var i = 0; i<arrNodes.length; i++) {
		textValue[i] = arrNodes[i].firstChild;
	}
};

Then on each page you can have a text box and fill it with:

Code:
_root.mytextbox.text += textValue[1];

...etc

Remember that arrays start at index zero not one so if you want to pull out the 4th element you need to use textValue[3] and so on.
 
Mate, It works like a charm. Thanks for everything and if there is ever anything I can do for you just let me know. See you 'round.
-Keyper.

The Key is more powerful than the Sword.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top