查明 AS3 的 XMLList 是否包含字符串作为节点值

发布于 2024-09-11 05:24:54 字数 724 浏览 0 评论 0原文

是否有与 Array.indexOf 等效的 XMLList?

例如——

var array:Array = ['one','two'];
trace(array.indexOf('two')); // returns 1, since it's at the second position
trace(array.indexOf('three')); // returns -1, since it isn't found

……对吧?但是如果我得到了这个该怎么办 -

var xml:XML = <list><item>one</item><item>two</item><list>
var xmlList:XMLList = list.item;

必须有一种更简单的方法来检查 XMLList 中的某个节点是否具有特定值,而不是循环遍历所有节点,对吗?类似于 -

xmlList.indexOfChildByNodeValue('two'); // returns 1, because it's the second child
xmlList.indexOfChildByNodeValue('three'); // returns -1, because it doesn't match any children

有道理吗?

Is there an XMLList equivalent to Array.indexOf?

For example -

var array:Array = ['one','two'];
trace(array.indexOf('two')); // returns 1, since it's at the second position
trace(array.indexOf('three')); // returns -1, since it isn't found

... right? but what if I've got this -

var xml:XML = <list><item>one</item><item>two</item><list>
var xmlList:XMLList = list.item;

there's got to be an easier way to check to see if one of the nodes in the XMLList has a particular value than looping through all of them, right? something akin to -

xmlList.indexOfChildByNodeValue('two'); // returns 1, because it's the second child
xmlList.indexOfChildByNodeValue('three'); // returns -1, because it doesn't match any children

make sense?

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

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

发布评论

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

评论(2

左秋 2024-09-18 05:24:54

我整理了一个演示,展示如何在不循环的情况下完成此操作。尽管它确实需要更多的前期工作。不过,如果您要经常检查值,那么这是有回报的。

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" applicationComplete="_init();">
    <fx:Declarations>
        <fx:XML id="movieList" source="http://www.movies.com/rss-feeds/jen-yamato-reviews-rss" />
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
            import mx.logging.ILogger;
            import mx.logging.Log;
            import mx.logging.LogEventLevel;
            import mx.logging.targets.TraceTarget;

            public static const MOVIE_TITLE:String = "Inception";

            private var _logger:ILogger = Log.getLogger("Sandbox");

            private function _init() :void
            {
                var traceTarget:TraceTarget = new TraceTarget();
                traceTarget.level = LogEventLevel.ALL;
                Log.addTarget(traceTarget);

                var xmlList:XMLList = movieList..item;

                var firstItem:XML = movieList..item[0];
                var firstItemIndex:int = firstItem.childIndex();
                _logger.info("First Item Index: " + firstItemIndex);

                var item:XML = xmlList.(title == MOVIE_TITLE)[0];
                _logger.info("Contains: " + xmlList.contains(item) + " // " + item.childIndex());

                var actualIndex:int = (item.childIndex() - firstItemIndex);
                _logger.info("Actual Index: " + actualIndex);

                _logger.info("Result: " + xmlList[actualIndex]);
            }
        ]]>
    </fx:Script>

</s:Application>

基本思想是,存储第一个 item 的索引,然后使用 E4X 搜索从匹配结果的 childIndex() 中减去该索引。

I put together a demo of how you can do it without looping. Though it does call for a little more upfront work. Though it pays off if you are going to be checking values a lot.

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" applicationComplete="_init();">
    <fx:Declarations>
        <fx:XML id="movieList" source="http://www.movies.com/rss-feeds/jen-yamato-reviews-rss" />
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
            import mx.logging.ILogger;
            import mx.logging.Log;
            import mx.logging.LogEventLevel;
            import mx.logging.targets.TraceTarget;

            public static const MOVIE_TITLE:String = "Inception";

            private var _logger:ILogger = Log.getLogger("Sandbox");

            private function _init() :void
            {
                var traceTarget:TraceTarget = new TraceTarget();
                traceTarget.level = LogEventLevel.ALL;
                Log.addTarget(traceTarget);

                var xmlList:XMLList = movieList..item;

                var firstItem:XML = movieList..item[0];
                var firstItemIndex:int = firstItem.childIndex();
                _logger.info("First Item Index: " + firstItemIndex);

                var item:XML = xmlList.(title == MOVIE_TITLE)[0];
                _logger.info("Contains: " + xmlList.contains(item) + " // " + item.childIndex());

                var actualIndex:int = (item.childIndex() - firstItemIndex);
                _logger.info("Actual Index: " + actualIndex);

                _logger.info("Result: " + xmlList[actualIndex]);
            }
        ]]>
    </fx:Script>

</s:Application>

The basic idea is that you store the index of the first item and then subtract that from the childIndex() of the matched result using E4X search.

情域 2024-09-18 05:24:54

我认为唯一的选择是循环遍历 XMLList。您的另一个选择是使用: 包含() 不过,如果您实际上只想知道是否 indexOf() == || ,这只会对您的情况有所帮助。 != -1

我不确定 XMLList 实际上是如何存储在底层 C++ 中的,但如果它确实只是一个数组,那么线性搜索 O(n) 是最好的选择。

I think the only bet is to loop through the XMLList. The other option you have is to use: contains() Though this will only help in your situation if you actually only want to know whether indexOf() == || != -1.

I am not sure of how the XMLList is actually stored in the underlying C++, but if it truly is just an array, then a linear search O(n) is the best you are going to do.

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