由 xml 填充的数组上的 indexOf
我有一个奇怪的问题。
当尝试在“静态”数组上执行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));
i have a strange issue.
When trying to do indexOf on a "static" array, it works, doing the same thing on an array populated with xml, i always get the result = -1
Static Way
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
Dynamic Way (from 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));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在将
XML
对象推送到数组,而不是字符串。如果您在属性上使用.toString()
,这应该可以工作。You are pushing an
XML
object to your array, not a string. This should work if you use.toString()
on the attribute.