如何判断 E4X 表达式是否匹配?

发布于 2024-07-17 07:04:49 字数 614 浏览 8 评论 0原文

我正在尝试访问 XMLList 项目并将其转换为 XML 对象。

我正在使用这个表达式:

masonicXML.item.(@style_number == styleNum)

例如,如果有匹配,则一切正常,但如果没有匹配,那么当我尝试将其转换为 XML 时,我会收到错误,说明它必须格式良好。 因此,我需要确保表达式在将其转换为 XML 之前获得匹配。 我尝试将其设置为 XMLList 变量并检查它是否作为 text() 属性,如下所示:

var defaultItem:XMLList = DataModel.instance.masonicXML.item.(@style_number == styleNum);
                    if(defaultItem.text())
                    {
                        DataModel.instance.selectedItem = XML(defaultItem);
                    }

但如果没有匹配项,它仍然会给我一个错误。 如果有匹配的话,效果很好。

谢谢!

I am trying to access an XMLList item and convert it to am XML object.

I am using this expression:

masonicXML.item.(@style_number == styleNum)

For example if there is a match everything works fine but if there is not a match then I get an error when I try cast it as XML saying that it has to be well formed. So I need to make sure that the expression gets a match before I cast it as XML. I tried setting it to an XMLList variable and checking if it as a text() propertie like this:

var defaultItem:XMLList = DataModel.instance.masonicXML.item.(@style_number == styleNum);
                    if(defaultItem.text())
                    {
                        DataModel.instance.selectedItem = XML(defaultItem);
                    }

But it still give me an error if theres no match. It works fine if there is a match.

THANKS!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

滥情哥ㄟ 2024-07-24 07:04:49

根据我的经验,检查结果的最简单方法是获取列表的第 0 个元素并查看它是否为 null

这是您的代码示例,经过一些调整。 请注意,我已将 defaultItem 的类型从 XMLList 更改为 XML,并将其分配给列表的第 0 个元素。

var defaultItem:XML = 
    DataModel.instance.masonicXML.item.(@style_number == styleNum)[0];
if( defaultItem != null ) 
{
    DataModel.instance.selectedItem = defaultItem;
}

In my experience, the simplest way to check for results is to grab the 0th element of the list and see if it's null.

Here is your code sample with a few tweaks. Notice that I've changed the type of defaultItem from XMLList to XML, and I'm assigning it to the 0th element of the list.

var defaultItem:XML = 
    DataModel.instance.masonicXML.item.(@style_number == styleNum)[0];
if( defaultItem != null ) 
{
    DataModel.instance.selectedItem = defaultItem;
}
风蛊 2024-07-24 07:04:49

好的,我让它与这个一起工作:

if(String(defaultItem.@style_number).length)

OK I got it to work with this:

if(String(defaultItem.@style_number).length)
被你宠の有点坏 2024-07-24 07:04:49

马特的空检查是一个很好的解决方案。 (除非 XMLList 中可能存在空项。可能不会,但我还没有验证这一点。)

您还可以检查 XMLList 的长度而不将其转换为字符串:

if (defaultItem.@style_number.length() > 0)

字符串和数组的区别问题在于,对于 XMLList,length() 是一种方法而不是属性。

Matt's null check is a good solution. (Unless there is the possibility of having null items within an XMLList.. probably not, but I haven't verified this.)

You can also check for the length of the XMLList without casting it to a String:

if (defaultItem.@style_number.length() > 0)

The difference to String and Array is that with an XMLList, length() is a method instead of a property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文