开发者

AS2 XML Parsing, Loops, Arrays & Objects

开发者 https://www.devze.com 2023-03-17 22:26 出处:网络
I want to parse an XML file with AS2, but I\'m not sure how to put the retrieved values into an array of objects with 开发者_运维百科the number of keys matching found values. The code has if statement

I want to parse an XML file with AS2, but I'm not sure how to put the retrieved values into an array of objects with 开发者_运维百科the number of keys matching found values. The code has if statements to filter out unwanted elements, but the number of array keys will always equal the length limit of the for loop.

How do I create an array of objects that only contains values that are not undefined?

//AS2

var xml:XML = new XML();
xml.ignoreWhite=true;

xml.onLoad = function(success)
{
    if (success) 
    {
        var myImage = xml.firstChild.childNodes;
        var img_arr = new Array();

        for (i=0; i<myImage.length; i++) 
        {
            img_arr[i] = new Object();

            var nodeName = xml.firstChild.childNodes[i].NodeName;


            //condition to filter out values
            if (nodeName == "value")
            {
                img_arr[i]["file"] = xml.firstChild.childNodes[i].firstChild.NodeValue;
                img_arr[i]["name"] = xml.firstChild.childNodes[i].childNodes[1].NodeValue;    
            }
        }
        //trace(img_arr.length)
    }
}
xml.load("test.xml");

Returns an Array like:

img_arr[file][0] = value0

img_arr[name][0] = name0

img_arr[file][1] = undefined

img_arr[name][1] = undefined

img_arr[file][2] = value2

img_arr[name][2] = name2

How do you get this array?

img_arr[file][0] = value0

img_arr[name][0] = name0

img_arr[file][1] = value2

img_arr[name][1] = name2


You could try to look for empty values before pushing them into your array.

Something like :

if (xml.firstChild.childNodes[i].firstChild.NodeValue.length > 0) {
    // here put the value into your array
}

Using the length of the value to find if its null or not, looking at them as String, can be useful sometimes : if its 0, that means that no character have been found in the nodeValue, showing that either the node doesn't exists, or the node is empty.

Using your code, here's what I suggest (must propably adapted to what you're doing, have not been tested) :

for (i=0; i<myImage.length; i++) {
    var nodeName:String = xml.firstChild.childNodes[i].NodeName;
    var newNode:Object;

    //condition to filter out values
    if (nodeName == "value") {
        // means that both of the nodes exists and contain a valid value
        if (xml.firstChild.childNodes[i].firstChild.NodeValue.length > 0 && xml.firstChild.childNodes[i].childNodes[1].NodeValue.length > 0) {
           newNode = new Object();
           newNode["file"] = xml.firstChild.childNodes[i].firstChild.NodeValue;
           newNode["name"] = xml.firstChild.childNodes[i].childNodes[1].NodeValue;
       }
    }
    if (newNode != null && newNode != undefined) {
        img_arr.push(newNode);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消