Flash AS3:XML 和未定义的值

发布于 2024-11-19 21:36:15 字数 819 浏览 5 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

肥爪爪 2024-11-26 21:36:16

在尝试提取 src 属性之前,您应该检查 resultsimg 元素是否存在。尝试这样做:

var imgRef:String;
if(groupPhotoXML.results && groupPhotoXML.results.img[0])
   imgRef = groupPhotoXML.results.img[0].@src;
else
   // The response was invalid

Before you attempt to extract the src attribute you should check to see if the results and img elements exist. Try doing this:

var imgRef:String;
if(groupPhotoXML.results && groupPhotoXML.results.img[0])
   imgRef = groupPhotoXML.results.img[0].@src;
else
   // The response was invalid
倚栏听风 2024-11-26 21:36:16

当跟踪为空时,您可能会获得正确的节点,但您需要使用 toXMLString() 查看它。

您可以通过检查 XMLList 的长度来检查 img 节点是否存在:

if(groupPhotoXML.results.img.length() > 0) trace('do something with the img nodes');

或者,如果您只需要没有空 src 属性的 img 节点,您可以像@taskinoor 建议的那样检查空字符串,或者通过检查@src的长度:

imgs:XMLList = groupPhotoXML.results.img;
for each(var img:XML in imgs) if([email protected]().length > 0) trace('valid node',[email protected]());

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:

if(groupPhotoXML.results.img.length() > 0) trace('do something with the img nodes');

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:

imgs:XMLList = groupPhotoXML.results.img;
for each(var img:XML in imgs) if([email protected]().length > 0) trace('valid node',[email protected]());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文