我有一个奇怪的问题。
当尝试在“静态”数组上执行indexOf时,它可以工作,在填充xml的数组上做同样的事情,我总是得到结果= -1
静态方式
var myarray:Array = new Array("Pub Pepsi","Shadow E-Bike","Icon Air Craft","Test","La page 4","La page 5","La page 6","La page 7");
var myresult:* = myarray.indexOf("Icon Air Craft");
trace(myresult);
// I get 2 as result so it works
动态方式(来自xml)
var Monthpath:String = "juin";
var XMLpathUrl:String = "http://www.omagazine.fr/editions/2011/" + Monthpath + "/obook_content.xml.php";
var xml:XML;
var xmlLoader:URLLoader = new URLLoader();
var myarray:Array = new Array();
xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);
function xmlLoaded(event:Event):void
{
xmlLoader.removeEventListener(Event.COMPLETE, xmlLoaded);
xmlLoader.removeEventListener(IOErrorEvent.IO_ERROR, xmlError);
xml = new XML(event.target.data);
for (var i in xml.pages.page)
{
myarray.push(xml.pages.page[i].@ga);
}
var myresult:* = myarray.indexOf("Icon Air Craft");
trace(myresult);
//-------------------------------------
// I get -1 as result!!!! Why????
//-------------------------------------
}
xmlLoader.addEventListener(IOErrorEvent.IO_ERROR, xmlError);
function xmlError(event:IOError):void
{
trace("Error");
}
xmlLoader.load(new URLRequest(XMLpathUrl));
答案 0 :(得分:3)
您正在将XML
对象推送到数组,而不是字符串。如果您对属性使用.toString()
,这应该有效。
for (var i in xml.pages.page)
{
myarray.push(xml.pages.page[i].@ga.toString());
}
var myresult:* = myarray.indexOf("Icon Air Craft");
trace(myresult);