如何判断 E4X 表达式是否匹配?
我正在尝试访问 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据我的经验,检查结果的最简单方法是获取列表的第 0 个元素并查看它是否为
null
。这是您的代码示例,经过一些调整。 请注意,我已将
defaultItem
的类型从XMLList
更改为XML
,并将其分配给列表的第 0 个元素。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
fromXMLList
toXML
, and I'm assigning it to the 0th element of the list.好的,我让它与这个一起工作:
OK I got it to work with this:
马特的空检查是一个很好的解决方案。 (除非 XMLList 中可能存在空项。可能不会,但我还没有验证这一点。)
您还可以检查 XMLList 的长度而不将其转换为字符串:
字符串和数组的区别问题在于,对于 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:
The difference to String and Array is that with an XMLList,
length()
is a method instead of a property.