Does anyone know anything about XML in Flash?

Megan's picture

She has: 11,421 posts

Joined: Jun 1999

Just thought I'd ask before I go into my whole problem. I'm trying to build a glossary with an XML backend but I keep getting stuck. I've tried a bunch of different sample codes to start with from various sites and while I can get some of them to display the data I keep getting stuck at a certain point.

Let me know if you might be able to help and I'll go into more detail.

They have: 5,633 posts

Joined: Jan 1970

I've never touched it before but I hear its easy to do.

I planned on getting to it in the future. Actually, let me put togeather a test. You useing Flash 8 or MX 2004?

Megan's picture

She has: 11,421 posts

Joined: Jun 1999

I'm sorry, Bryan, I need some one who actually knows how to do this. I can get the data in and display it, what I'm having trouble with is getting it into a form I can use.

demonhale's picture

He has: 3,278 posts

Joined: May 2005

what exactly are you trying to do meg?, im not really good at xml flash combo but I have some progrmaming background and maybe I can help...

JeevesBond's picture

He has: 3,956 posts

Joined: Jun 2002

So how far have you got and what do you actually need this thing to do? It seems that you have some code already, mind posting your code?

They have: 5,633 posts

Joined: Jan 1970

um... what form do you need it in?
Don't under estamet me, I learned enough C# to write a website log in 1 month without any refrence other than google. (and what little MSDN i understood back then)

well that and I know 1 of like 100 people in the country that are actionscript certified. Wink

JeevesBond's picture

He has: 3,956 posts

Joined: Jun 2002

Well put the code up and we'll all take a look at it, first one with the correct answer gets a date with Megan. Laughing out loud

demonhale's picture

He has: 3,278 posts

Joined: May 2005

Whohoooo! Im In!!! LoL!

JeevesBond's picture

He has: 3,956 posts

Joined: Jun 2002

Well I'm not sure how Megan herself will react, but then everyone should be used to my flippent comments by now. Smiling

What I think's funny is how the title is abbreviated on the main page: "Does anyone know anything..." I only went to this thread to answer "no."

a Padded Cell our articles site!

They have: 5,633 posts

Joined: Jan 1970

lol @ Jeeves

Well, I was going to help but now the prize was offered I might not answer. Wink

timjpriebe's picture

He has: 2,667 posts

Joined: Dec 2004

Poor Megan. She just posted wanting some honest help from someone with experience, and everyone posted a reply with no real help (myself and this reply included).

A date with Megan, huh? I would say that's quite a distance for a date, but the rumor mill says it's worth the commute... Smiling

JeevesBond's picture

He has: 3,956 posts

Joined: Jun 2002

Subtle Tim, I like it. Wink

I can imagine it is worth it, anyway I'm only being mean because, 1. I'm waiting to see some source code. 2. We all know Megan (or should do after 5,627 posts) and can - hopefully - get away with being a bit silly. Laughing out loud

a Padded Cell our articles site!

Megan's picture

She has: 11,421 posts

Joined: Jun 1999

LOL Smiling

Here's the code I've got right now, with verbose coments Smiling Actionscript is really almost the same as Javascript so some of you might be able to help. I know Jeeves knows a lot about javascript!

The problem right now is that it doesn't seem to be adding values to the array, it looks like it's resetting it... I've tried push and concat there and neither does anything. Acutally, push doesn't do anything, concat at least returns something.

// The first step is to activate the XML object
glossaryXML = new XML();
/*
With the XML Object now active you must now load an XML foramtted document.
Any DTD or XLS formatting will be ignored.
*/
glossaryXML.onLoad = myLoad;
glossaryXML.load("../../glossary.xml");
glossaryXML.ignoreWhite = true; // strips white space from the XML document
// Before proceeding to far into the program, make sure the XML document has loaded
// Extract information from the XML file
function myLoad(ok) {
if (ok == true) {
Publish(this.firstChild);
}
}

function Publish(glossaryXMLNode) {
if (glossaryXMLNode.nodeName == "glossary") {
content = "";
glossaryitem = glossaryXMLNode.firstChild;
while (glossaryitem != null) {
if (glossaryitem.nodeName == "glossaryitem") { 
// I don't really think all these if statements are necessary.
// It should be possible just to get the value of the nodes without conditionals.
term = "";
definition = "";
term_array = new Array();
definition_array = new Array(); // not sure if I want separate arrays here (???)
element = glossaryitem.firstChild;
while (element != null) {
// looping through the glossary items to find the terms and definitions
if (element.nodeName == "term") {
term = element.firstChild.nodeValue;
term_array = term_array.concat(element.firstChild.nodeValue);
// add term to term array; doesn't seem to be working right now.
termArray_txt.htmltext=term_array[0];
//returning the value of the last item (not what should be the first item!)
trace(termArray);
trace("testing trace comment");

}
if (element.nodeName == "definition") {
definition = element.firstChild.nodeValue;
// add item to definition array
definition_array = definition_array.concat(element.firstChild.nodeValue);
definitionArray_txt.htmltext=definition_array[0];
//this is returning the value of the last item (not what should be the first item!)
}
element = element.nextSibling; // goes on to the next item
}
content += term+"<br>"+definition+"<br><br>";
// compliles the terms and definitions into the content variable
content_txt.htmltext=content; // writes to the text field (this works fine)

}
glossaryitem = glossaryitem.nextSibling; // goes on to the next item
}
}
}

function findTerm(termText) {
// what I'll need to do here is loop through the arrays to find the right term and the matching definition


term_txt.htmltext=term; // for testing - set the value of the term_txt textbox to term value
definition_txt.htmltext=definition;  // set the value of definition text box to definition value
// these currently return the last item in the list.


// once that's working we'll need to create a tooltip to put it in instead of the current text boxes.

}
'My other problem is that my trace(); doesn't seem to be working so I can't track what's going on. I have the filter level set to verbose so I don't know what the problem could be with that.

JeevesBond's picture

He has: 3,956 posts

Joined: Jun 2002

The date shall be mine!!!

Got the XML file that this is parsing?

Megan's picture

She has: 11,421 posts

Joined: Jun 1999

<glossary>

<glossaryitem>
<term>Hinduism</term>
<definition>Hinduisim is a religion proiminent in South Asia</definition>
</glossaryitem>

<glossaryitem>
<term>Buddhism</term>
<definition>Buddhism is a religion proiminent in East Asia</definition>
</glossaryitem>

<glossaryitem>
<term>Shiva</term>
<definition>I don't really know about Shiva</definition>
</glossaryitem>

<glossaryitem>
<term>Anomie</term>
<definition>Social instability resulting from a breakdown of standards and values</definition>
</glossaryitem>

</glossary>
'
Here is the flash file: http://admmail.uwaterloo.ca/~mjjack/glossary.fla

Note that the XML is stored up one level.

demonhale's picture

He has: 3,278 posts

Joined: May 2005

dictionary... A side note: (not really helpful) but why use xml flash when this can be done via php mysql...

Megan's picture

She has: 11,421 posts

Joined: Jun 1999

Firstly, because it's not a server-side app. Secondly, because I think XML is a better way of sorting the data.

They have: 5,633 posts

Joined: Jan 1970

Never dought the power of xml!!

I'll get right on that solution...

They have: 5,633 posts

Joined: Jan 1970

It helps if you keep the var the same name.
It is declared as "termArray"
But you start useing "term_array"

This is working...

var term:String = "";
var definition:String = "";
var termArray = new Array();
var definition_array = new Array(); // not sure if I want separate arrays here (???)
var element = glossaryitem.firstChild;
while (element != null) {
// looping through the glossary items to find the terms and definitions
if (element.nodeName == "term") {
term = element.firstChild.nodeValue;
termArray = termArray.concat(element.firstChild.nodeValue);
// add term to term array; doesn't seem to be working right now.
termArray_txt.htmltext = termArray[0];
//returning the value of the last item (not what should be the first item!)
trace(termArray[0]);
trace(" - - - ");

}
'

Megan's picture

She has: 11,421 posts

Joined: Jun 1999

Um, no I didn't. termArray_txt is the name of the text box, term_array is the name of the array. And that code does not work, it basically does the same thing mine did except that it doesn't return a definition so it's actually worse.

Please, Bryan, could you please just leave it alone. Trying to help with something you don't know about is not helpful.

They have: 5,633 posts

Joined: Jan 1970

****, it seems fine to me http://proj.bja888.com/tests/glossary.swf.

JeevesBond's picture

He has: 3,956 posts

Joined: Jun 2002

Try this, I rewrote the lot as the first solution was restrictive and inelegant IMO, but then I am a purist. Smiling

This version allows the glossaryitem to appear anywhere, I even managed to stick it in the middle of a load of xhtml and it still worked, you can also split the glossary items and even nest them.

var CONST_NO_DEFINITION_ERROR:String = 'Error! The term was found in the glossary, but there was no description with it or the description was blank';
//Change the above to give a different error message when a term is found but a definition for it is not.
var objGlossary = new Object();
//Associative array for glossary terms
glossaryXML = new XML();
// Activate the XML object
/*
With the XML Object now active you must now load an XML foramtted document.
Any DTD or XSL formatting will be ignored.
*/

glossaryXML.onLoad = myLoad;
glossaryXML.load("../glossary.xml");

glossaryXML.ignoreWhite = true; // strips white space from the XML document

// Before proceeding too far into the program, make sure the XML document has loaded
// Extract information from the XML file
function myLoad(ok) {
if (ok == true) {
GetTerms(glossaryXML);
}
}

function GetTerms(xmlParse) {

for (var objNodeLoop = xmlParse.firstChild; objNodeLoop != null; objNodeLoop = objNodeLoop.nextSibling) {

//Following is responsible for traversing up and down in the XML tree
//Moves us down childNodes when they are available and reverses back up
//parentNodes when no more nodes or children are available on current level.

checkNode(objNodeLoop);

for (objNodeLoop = objNodeLoop.firstChild; objNodeLoop.firstChild != null; objNodeLoop = objNodeLoop.firstChild) {
//Loop through child nodes
checkNode(objNodeLoop);
}
//Head down tree as far as we can, checking all the way

while (objNodeLoop.nextSibling == null && objNodeLoop.parentNode != null) {
objNodeLoop = objNodeLoop.parentNode
}
//Head back up tree seaching for available siblings
}
}

function checkNode(objNode) {

//NOTE: This function interacts directly with objGlossary associative array.
//this is the reason it does not return a value.
//Only rules for placement of the desired elements within XML is that the term and definition
//must be children of glossaryitem and there cannot be more than one term or definition per
//glossaryitem, if there is the first value will be overwitten.

var strTerm = new String();
var strDefinition = new String();

if (objNode.nodeName == 'glossaryitem') {
//glossaryitem element found, search the children...

strTerm = '';
strDefinition = '';

for (var objGlossaryItems = objNode.firstChild;
objGlossaryItems != null;
objGlossaryItems = objGlossaryItems.nextSibling) {

if (objGlossaryItems.nodeName == 'term') { strTerm = objGlossaryItems.firstChild.nodeValue; }
if (objGlossaryItems.nodeName == 'definition') { strDefinition = objGlossaryItems.firstChild.nodeValue; }
//term or definition found, shove value into a string for later use.
}
if (strTerm != '' && strDefinition != '') {
objGlossary[strTerm] = strDefinition;
} else if (strTerm != '' && strDefinition == '') {
//If a term is defined but no matching Definition output an error message...
objGlossary[strTerm] = CONST_NO_DEFINITION_ERROR;
}
}
}

function findTerm(termText) {

term_txt.htmlText= termText;
definition_txt.htmlText= objGlossary[termText];

// we'll need to create a tooltip to put it in instead of the current text boxes.

}
'

What it doesn't seem to like is non-closing tags, but unless you really need me to, I can't be bothered to find out why. It'll just make the code less elegant anyway.

Apart from that it seems pretty robust, I even threw this ghastly rubbish at it and the thing gobbled it up quite happily. Smiling

<glossary>
<whatever><funtybunt>fhgeyfgerufgerufgre</funtybunt><glossaryitem>
<term>Buddhism<glossaryitem>
<term>Hinduism</term><glossaryitem>
<term>Anomie</term>
<definition>Social instability resulting from a breakdown of standards and values</definition>
</glossaryitem>
<definition>Hinduisim is a religion proiminent in South Asia</definition>
</glossaryitem></term>
<definition>Buddhism is a religion proiminent in East Asia</definition>
</glossaryitem></whatever>
<glossaryitem>
<term>Shiva</term>
<definition>I don't really know about Shiva</definition>
</glossaryitem>



</glossary>
'

a Padded Cell our articles site!

AttachmentSize
glossary.fla 43.03 KB
JeevesBond's picture

He has: 3,956 posts

Joined: Jun 2002

Oooops, I changed the path/filename in the fla to work with my setup, you'll need to change it back to: glossaryXML.load("../glossary.xml"); to make it work.

a Padded Cell our articles site!

Megan's picture

She has: 11,421 posts

Joined: Jun 1999

Okay, thanks, I'll try that on Monday when i get to work.

Thanks a lot!

They have: 5,633 posts

Joined: Jan 1970

I feel so unwelcome. I solved it 21 hours before Jeeves, kept most of your origional code and still you ingore me.

For future refrence
post what you want fixed "The problem right now is that it doesn't seem to be adding values to the array" doesn not say you need it to desplay in the boxes on click.

Megan's picture

She has: 11,421 posts

Joined: Jun 1999

The point of the question was to create the full functionality - I referenced the array problem as a possible source of the problem. The code you posted was incomplete and therefore didn't work for me. Flash actually choked on it when I put it in but that might have had something to do with the publish version.

They have: 5,633 posts

Joined: Jan 1970

I used studio 8 to work on it but I exported as 7.

You didn't write that code your self did you? Or read the comments?
I noticed it was missing a few key things to make it functional. I just figured you hadn't gotten around to that yet. So my primary concern was to get your trace functions working correctly. Next time you need the whole problem solved for you just say so.

Megan's picture

She has: 11,421 posts

Joined: Jun 1999

God, Bryan, that was implicit with the original question! Stop being a jerk about it, jeez.

JeevesBond's picture

He has: 3,956 posts

Joined: Jun 2002

So does this mean I win the date? Wink

I reckon we should turn this into a sordid love triangle thing and go on Jerry Springer.

They have: 5,633 posts

Joined: Jan 1970

Hey dont get me involved. I already have my happily ever after. I declined way at the beggnning to the thread.

JeevesBond's picture

He has: 3,956 posts

Joined: Jun 2002

Hmmmph, you declined in a tongue in cheek, damn I really want that date sort of way.

Anyway we could make some cash and raise the profile of TWF by going on there, all publicity is good!

a Padded Cell our articles site!

Megan's picture

She has: 11,421 posts

Joined: Jun 1999

I don't know, we'd probably need to throw a lesbian in the mix or something Roll eyes

Will look at that in a few minutes, Jeeves Smiling

What do you think would be the best way to display the definition? I could do a tooltip type thing if I can find some good code for it, otherwise just a plain pop-up box. I might need some help figuring out how to make the box the right size for the text.

Megan's picture

She has: 11,421 posts

Joined: Jun 1999

Whoo-hoo! It works! And I did the tool tips all by myself Laughing out loud

http://admmail.uwaterloo.ca/~mjjack/glossary/glossary.swf

Now I can dump the actionscript code into an external file.

Jeeves - if the term is not found it returns nothing. How can I put an error message in there for that?

Thanks a million for the code Smiling Would have rather done it myself but this is probably a much better solution.

JeevesBond's picture

He has: 3,956 posts

Joined: Jun 2002

var CONST_NOTHING_ERROR = 'Error! No term or definition found';
//Change the above to give a different error message when nothing is found.
var CONST_NO_DEFINITION_ERROR = 'Error! The term was found in the glossary, but there was no description with it or the description was blank';
//Change the above to give a different error message when a term is found but a definition for it is not.
var objGlossary = new Object();
//Associative array for glossary terms
glossaryXML = new XML();
// Activate the XML object
/*
With the XML Object now active you must now load an XML foramtted document.
Any DTD or XSL formatting will be ignored.
*/

glossaryXML.onLoad = myLoad;
glossaryXML.load("glossary2.xml");

glossaryXML.ignoreWhite = true; // strips white space from the XML document

// Before proceeding too far into the program, make sure the XML document has loaded
// Extract information from the XML file
function myLoad(ok) {
if (ok == true) {
GetTerms(glossaryXML);
}
}

function GetTerms(xmlParse) {

for (var objNodeLoop = xmlParse.firstChild; objNodeLoop != null; objNodeLoop = objNodeLoop.nextSibling) {

//Following is responsible for traversing up and down in the XML tree
//Moves us down childNodes when they are available and reverses back up
//parentNodes when no more nodes or children are available on current level.

checkNode(objNodeLoop);

for (objNodeLoop = objNodeLoop.firstChild; objNodeLoop.firstChild != null; objNodeLoop = objNodeLoop.firstChild) {
//Loop through child nodes
checkNode(objNodeLoop);
}
//Head down tree as far as we can, checking all the way

while (objNodeLoop.nextSibling == null && objNodeLoop.parentNode != null) {
objNodeLoop = objNodeLoop.parentNode
}
//Head back up tree seaching for available siblings
}
}

function checkNode(objNode) {

//NOTE: This function interacts directly with objGlossary associative array.
//this is the reason it does not return a value.
//Only rules for placement of the desired elements within XML is that the term and definition
//must be children of glossaryitem and there cannot be more than one term or definition per
//glossaryitem, if there is the first value will be overwitten.

var strTerm = new String();
var strDefinition = new String();

if (objNode.nodeName == 'glossaryitem') {
//glossaryitem element found, search the children...

strTerm = '';
strDefinition = '';

for (var objGlossaryItems = objNode.firstChild;
objGlossaryItems != null;
objGlossaryItems = objGlossaryItems.nextSibling) {

if (objGlossaryItems.nodeName == 'term') { strTerm = objGlossaryItems.firstChild.nodeValue; }
if (objGlossaryItems.nodeName == 'definition') { strDefinition = objGlossaryItems.firstChild.nodeValue; }
//term or definition found, shove value into a string for later use.
}
if (strTerm != '' && strDefinition != '') {
objGlossary[strTerm] = strDefinition;
} else if (strTerm != '' && strDefinition == '') {
//If a term is defined but no matching Definition output an error message...
objGlossary[strTerm] = CONST_NO_DEFINITION_ERROR;
}
}
}

function findTerm(termText) {

term_txt.htmlText= termText;
if (objGlossary[termText] == null) {
definition_txt.htmlText = CONST_NOTHING_ERROR;
} else {
definition_txt.htmlText = objGlossary[termText];
}

// we'll need to create a tooltip to put it in instead of the current text boxes.

}
'
Version 2, sorry I took so long!
There's a new constant: CONST_NOTHING_ERROR
And a new conditional in findTerm(termText) that checks to see if the return from the array is blank and sets the definition to CONST_NOTHING_ERROR if it is and outputs the glossary entry if it isn't.

a Padded Cell our articles site!

AttachmentSize
glossary.fla 7.13 KB
Megan's picture

She has: 11,421 posts

Joined: Jun 1999

More problems - I got it put into my player and everything seemed fine but now I've got instances where I've got several glossary items on a page and some work but others don't. I've checked the spelling and the order of items on the flash screen and everything seems fine.

Here is an example:

http://admmail.uwaterloo.ca/~mjjack/glossary/medieval_philos.html

If you go to the very end of the timeline you can see that A=B works, but Nirguna Brahman doesn't. Atman works (it's off the edge of the screen but I think I can fix that) but the rest don't.

These are now being loaded into the player which is a separate movie. I had to put the actionscript calls into that file instead of the regular movie. I hope this all makes sense! All the files are on my web space here:

http://admmail.uwaterloo.ca/~mjjack/

Any help? (I know this is a pretty big question!)

Megan's picture

She has: 11,421 posts

Joined: Jun 1999

Update: Seems to be a problem with the XML. Looks like it's handling everything before D in the alphabetical order, but then has problems after that. Is there anything in my xml around the "D" area that would be causing a problem? I tried taking out the whole D section and that seems to correct the problem.

Whew, that's a relief actually! Minor problem Smiling

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.