0

我有一些相当大的 XML 文件,我需要在 Flash 中进行比较,然后为每个匹配的节点分配不同的分数。我无法控制 XML 文件,所以我不能使用节点属性或类似的东西来存储点值。在下面的简化示例中,我正在考虑使用多维数组来存储节点位置,然后是类似于下面的点值。虽然它没有返回错误,但它显然没有按照我想要的方式工作,因为点值应该只有 5。任何帮助将不胜感激。

var sourceXML:XML = 
            <root>
                <white>
                    <player>
                        <name>DOES NOT MATCH</name>
                        <sport>Basketball</sport>
                    </player>
                    <player>
                        <name>John Johnson</name>
                        <sport>Soccer</sport>
                    </player>
                    <player>
                        <name>James Anderson</name>
                        <sport>Tennis</sport>
                    </player>
                </white>
                <black>
                    <player>
                        <name>John Smith</name>
                        <sport>Soccer</sport>
                    </player>
                    <player>
                        <name>Frank Johnson</name>
                        <sport>Soccer</sport>
                    </player>
                    <player>
                        <name>Peter Jackson</name>
                        <sport>Baseball</sport>
                    </player>
                </black>
            </root>

    var comparedXML:XML = 
                <root>
                    <white>
                        <player>
                            <name>Steve Smith</name>
                            <sport>Basketball</sport>
                        </player>
                        <player>
                            <name>John Johnson</name>
                            <sport>Soccer</sport>
                        </player>
                        <player>
                            <name>James Anderson</name>
                            <sport>Tennis</sport>
                        </player>
                    </white>
                    <black>
                        <player>
                            <name>John Smith</name>
                            <sport>Soccer</sport>
                        </player>
                        <player>
                            <name>Frank Johnson</name>
                            <sport>Soccer</sport>
                        </player>
                        <player>
                            <name>Peter Jackson</name>
                            <sport>Baseball</sport>
                        </player>
                    </black>
                </root>

    trace(sourceXML.white.player.name[0]); //DOES NOT MATCH
    trace(comparedXML.white.player.name[0]); //Steve Smith

    var scored:Array = new Array();
    scored[0] = [".white.player.name[0]", "100"];
    scored[1] = [".white.player.name[1]", "1"];
    scored[2] = [".white.player.name[2]", "1"];
    scored[3] = [".white.player.sport[0]", "1"];
    scored[4] = [".white.player.sport[1]", "1"];
    scored[5] = [".white.player.sport[2]", "1"];

    var points:Number = 0;

    for(var i:int = 0; i < scored.length; i++)
    {
        if(sourceXML[scored[i][0]] == comparedXML[scored[i][0]])
        {
            points += Number(scored[i][1]);
        }
    }

    trace(points);
4

1 回答 1

0

您不能像您在此处尝试的方式那样遍历 XML 中的多个属性 sourceXML[scored[i][0]]

为什么不直接将他们的得分存储在 XML 中,然后在循环中遍历两个 XML 列表?由于维护代码似乎非常困难,因此您添加的每个条目都会添加大量代码。

于 2013-06-14T18:16:25.880 回答