Hello,
I’m using the basic XML tutorial created by Kirupa. I want to use XML to load vocabulary words and definitions into Flash, and also display them online as an XML page. The problem is that when I add the bold or italics tags, these are treated as new nodes and mess up the function that calls the information. See the code I’m using below. I’ve looked around and cannot figure out how to fix this. Any help would be appreciated.
function loadXML(loaded) {
if (loaded) {
_root.inventor = this.firstChild.childNodes[0].childNodes[0].firstChild.nodeValue;
_root.comments = this.firstChild.childNodes[0].childNodes[1].firstChild.nodeValue;
name_txt.htmlText = _root.inventor;
comment_txt.text = _root.comments;
} else {
trace("file not loaded!");
}
}
xmlData = new XML();
xmlData.ignoreWhite = true;
xmlData.onLoad = loadXML;
xmlData.load("investors.xml");
pulling from this xml file
<?xml version="1.0"?>
<inventors>
<person>
<name>Thomas <b>Edison</b></name>
<comment>Inventor of many things such as the incandescent lightbulb.</comment>
</person>
<person>
<name>Doug Engelbart</name>
<comment>Invented the mouse at the Stanford Research Institute</comment>
</person>
</inventors>
a year later and I’m back on this issue…
I can use CDATA tags for the nodes that include the <em> subnodes but when I transform the XML to HTML the < and > symbols show up in the code as < and >, therefore the <em> tags show up in the online display.
node:
<lesson01 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../vocabulary.xsd">
<term>
<word>descendant of Ham</word>
<context><![CDATA[". . . and if their increase will do no other good, it will do away the force of argument, that God cursed <em>Ham</em>, and therefore American slavery is right." (22)]]></context>
<definition>in the Bible, descendant of the second son of Noah who fathered the black race</definition>
</lesson01>
</term>
and this is how I’m calling it in Flash
unction loadXML(loaded) {
if (loaded) {
xmlNode = this.firstChild;
term = [];
context = [];
definition = [];
_global.total = xmlNode.childNodes.length;
trace("global.total = "+_global.total);
for (i=0; i<total; i++) {
term* = xmlNode.childNodes*.childNodes[0].firstChild.nodeValue;
context* = xmlNode.childNodes*.childNodes[1].firstChild.nodeValue;
definition* = xmlNode.childNodes*.childNodes[2].firstChild.nodeValue;
trace("The name of this term is "+term*);
trace("Context: "+context*);
trace("Definition: "+definition*);
}
gotoAndStop("main", 1);
} else {
content = "file not loaded!";
}
}
Anybody have any ideas on this one?
OK, I found a work around…get rid of the CDATA tags… When loading the XML into arrays check to see if the node has childNodes (the italic tags) and if it does, add each of these to the array entry, here’s the code:
_global.context_nodes = xmlNode.childNodes*.
childNodes[1].childNodes.length;
if (_global.context_nodes == 3) {
context* = (xmlNode.childNodes*.childNodes[1].
firstChild.nodeValue) + (xmlNode.childNodes*.
childNodes[1].childNodes[0].nextSibling) +
(xmlNode.childNodes*.childNodes[1].childNodes[1].
nextSibling);
} else if (_global.context_nodes == 5) {
context* = (xmlNode.childNodes*.childNodes[1].
firstChild.nodeValue) + (xmlNode.childNodes*.
childNodes[1].childNodes[0].nextSibling) + (xmlNode.
childNodes*.childNodes[1].childNodes[1].nextSibling)+
(xmlNode.childNodes*.childNodes[1].childNodes[2].
nextSibling) + (xmlNode.childNodes*.childNodes[1].
childNodes[3].nextSibling);
} else {
context* = xmlNode.childNodes*.childNodes[1].
firstChild.nodeValue;
}