我有来自flashkit的以下词汇表as2 XML代码示例,当我尝试跟踪以查看如何从XML加载文本时,我无法得到代码实际上如何找到正确的加载条目。当我执行跟踪时,它返回完整的XML条目数组而不是单个数组。
_master.termByID = function(termID:String) {
displayTermByID(termID);
};
function displayTermByID(termID:String):Void {
var info:String = "";
var originalTerm = termID;
var found = findTermIndex(termID);
trace("im here");
while(found == -1 && termID.length > 0){
info = "The referenced term ID <i>"+originalTerm+"</i> was not found. <br>";
trace("info:"+info);
found = findTermIndex(termID);
trace("found:"+found);
termID=termID.substr(0,termID.length-1);
trace("termID:"+termID);
}
if(found != -1 && originalTerm != termID){
info += "Displaying closest matches: " + termID + "<br>";
} else if (termID.length ==0){
info += "No substring yielded any matches.<br>";
}
whichTerm = found;
updateInterface(info);
}
这是用于显示文本的主要方法,但是当我执行跟踪时,它甚至没有显示在控制台中,但文本正在文本框中显示。
function findTermIndex(termID:String):Number {
var pow:Number = 2;
var numTerms = termArray.length;
var split:Number = Math.round(numTerms/pow);
var currId = termArray[split].attributes.id;
while (currId != termID && numTerms/pow>.5) {
pow *= 2;
if (currId<termID) {
split += Math.round(numTerms/pow);
} else {
split -= Math.round(numTerms/pow);
}
currId = termArray[split].attributes.id;
}
if (currId == termID) {
return (split);
} else {
return (-1);
}
}
这用于查找术语索引。
function parse(glosSrc:XML):Void {
var numTerms = glosSrc.childNodes.length;
for (var i = 0; i<numTerms; i++) {
var thisChild = glosSrc.childNodes[i];
var insertPoint = termArray.length;
while (insertPoint>0 && termArray[insertPoint-1].attributes.id>thisChild.attributes.id) {
insertPoint--;
}
termArray.splice(insertPoint, 0, thisChild);
// use this to get the imgURL value
//trace(glosSrc.childNodes[i].childNodes[1].firstChild.nodeValue);
//thisImg = String(glosSrc.childNodes[i].childNodes[1].firstChild.nodeValue);
}
searchTerms("");
//imgLoader.contentPath = thisImg;
}
XML解析函数
function displaySearchResults(matches:Array):Void {
var numMatches = matches.length;
if (numMatches) {
var resultString = '<textformat leading="'+resultLineSpacing+'"><font size="'+fontSize+'">';
for (var i = 0; i<numMatches; i++) {
resultString += '<a href="asfunction:displayTermByIndex,'+matches[i]+'">';
var fullTerm = termArray[matches[i]].firstChild.firstChild.firstChild.nodeValue;
var cleanTerm = escapeAtChar(fullTerm);
resultString += cleanTerm+'</a><br>';
//trace(resultString);
}
resultString += '</font></textformat>';
//def.setTextFormat(resultFormatting);
def.htmlText = resultString;
//trace(resultString);
} else {
def.htmlText = '<font color="#'+errorHexColor+'" size="'+(fontSize+2)+'">No matching terms found.</font>';
}
gBoxPath.onComplete();
}
另一种用于显示XML结果文本的方法。 def是用于显示文本的动态文本框对象。
所以使用所有这些方法,我已经跟踪了所有可能传递的字符串但是所有字符串似乎都没有返回任何内容,或者它返回了很多参数而不是应该显示的参数。
如果有人可以解释代码实际如何工作,那么我会很高兴,所以我在XML中添加更多字段。这里有整个源文件。