0

I'm creating an action script 3 (Flash CS5) application. I have a 2D array like this:

var data:Array = new Array(5);
data[0] = new Array(5);
data[0] = new Array(2);
data[0] = new Array(6);
data[0] = new Array(4);
data[0] = new Array(1);

how can i serialize it az XML instance and save data in it. for example like this:

<page id="1">
    <item id="1">
    </item>
    <item id="2">
    </item>
    <item id="3">
    </item>
    <item id="4">
    </item>
    <item id="5">
    </item>
</page>
<page id="2">
    <item id="1">
    </item>
    <item id="2">
    </item>
</page>
<page id="3">
    <item id="1">
    </item>
    <item id="2">
    </item>
    <item id="3">
    </item>
    <item id="4">
    </item>
    <item id="5">
    </item>
    <item id="6">
    </item>
</page>
<page id="4">
    <item id="1">
    </item>
    <item id="2">
    </item>
    <item id="3">
    </item>
    <item id="4">
    </item>
</page>
<page id="5">
    <item id="1">
    </item>
</page>

Any sample code will be greate! Thanks in advance :D

1 Answer 1

3

Sample code:

    var data:Array = [
        ["apple", "orange", "banana"],
        ["dog", "cat", "monkey", "lion"],
        ["venus", "mars"]
    ];

    var xml:XML = <xml />;

    for (var i:int = 0; i < data.length; i++) {
        var page:Array = data[i];

        var pageNode:XML = <page />;
        pageNode.@id = i;

        xml.appendChild(pageNode);

        for (var j:int = 0; j < page.length; j++) {
            var item:String = page[j];

            var itemNode:XML = <item />;
            itemNode.@id = j;

            pageNode.appendChild(itemNode);

            itemNode.appendChild(item);
        }
    }

    trace("XML:", xml.toString());

Output:

XML: <xml>
  <page id="0">
    <item id="0">apple</item>
    <item id="1">orange</item>
    <item id="2">banana</item>
  </page>
  <page id="1">
    <item id="0">dog</item>
    <item id="1">cat</item>
    <item id="2">monkey</item>
    <item id="3">lion</item>
  </page>
  <page id="2">
    <item id="0">venus</item>
    <item id="1">mars</item>
  </page>
</xml>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Exactly what i want! :x

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.