Flash AS3:XML 和未定义的值
我正在使用 YQL 尝试为我正在开展的项目查找乐队照片。如果找到图像,则会在 XML 响应中返回该图像,然后我的 Flash 代码将其发布。但如果没有找到,我不太确定如何告诉闪光灯不要做任何事情。这是我的代码:
XML.ignoreWhitespace = true;
var groupPhotoXML = new XML (e.target.data);
var imgRef = groupPhotoXML.results.img[0].@src;
if (imgRef != undefined){
//DO STUFF. THIS IF STATEMENT DOESN'T WORK THOUGH. ALSO TRIED if(groupPhotoXML != undefined)
}
这是成功的 grouPhotoXML 响应的样子:
<query yahoo:count="1" yahoo:created="2011-07-13T16:35:21Z" yahoo:lang="en-US" xmlns:yahoo="http://www.yahooapis.com/v1/base.rng">
<results>
<img alt="Syn photo" id="artistArtistImg" src="http://d.yimg.com/ec/image/v1/artist/266256;encoding=jpg;size=156x94" title="Syn photo"/>
</results>
</query>
不成功的尝试将显示为空。
I'm using YQL to try and find band photos for a project I'm working on. If an image is found, it is returned in an XML response, and my flash code posts it. But if one is not found, I'm not quite sure how to tell flash not to do anything. Here's my code:
XML.ignoreWhitespace = true;
var groupPhotoXML = new XML (e.target.data);
var imgRef = groupPhotoXML.results.img[0].@src;
if (imgRef != undefined){
//DO STUFF. THIS IF STATEMENT DOESN'T WORK THOUGH. ALSO TRIED if(groupPhotoXML != undefined)
}
Here's what a successful grouPhotoXML response looks like:
<query yahoo:count="1" yahoo:created="2011-07-13T16:35:21Z" yahoo:lang="en-US" xmlns:yahoo="http://www.yahooapis.com/v1/base.rng">
<results>
<img alt="Syn photo" id="artistArtistImg" src="http://d.yimg.com/ec/image/v1/artist/266256;encoding=jpg;size=156x94" title="Syn photo"/>
</results>
</query>
An unsuccessful attempt traces out as empty.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在尝试提取
src
属性之前,您应该检查results
和img
元素是否存在。尝试这样做:Before you attempt to extract the
src
attribute you should check to see if theresults
andimg
elements exist. Try doing this:当跟踪为空时,您可能会获得正确的节点,但您需要使用 toXMLString() 查看它。
您可以通过检查 XMLList 的长度来检查 img 节点是否存在:
或者,如果您只需要没有空 src 属性的 img 节点,您可以像@taskinoor 建议的那样检查空字符串,或者通过检查@src的长度:
You might get the right node when the traces are empty, but you need to use toXMLString() to see it.
You can either check if the img node is present by checking the length of the XMLList:
or, if you only need img nodes that do not have an empty src attribute, you can check for an empty string like @taskinoor suggests, or by checking the length of @src: