Search code examples
xmlactionscript-3aireditingsave

Air As3 XML editing/saving


I need to make a class that simplifies saving dynamic/input TextField text to an XML file. I've got it saving the XML object from as3 to a file on the file system without problems. Basically what I need is a generic class that can be given a DisplayObject to have all of its TexField's set to the data in XML. I need a way of linking the TextField to its data. I'm using Air 2.0.


Solution

  • I'm not sure I totally understand your question but you could try looping through all the children of the display object, checking for textFields, and if you find them, write them out to XML. Something like this...

    function writeChildTextFieldsToXML(xml:XML, container:DisplayObjectContainer):void {
        for (var i:int = 0; i < container.numChildren; i++) {
            var child:DisplayObject = container.getChildAt(i);
            if (child is TextField) {
                var text:String = TextField(child).text;
                // Write text to xml
            }
            if (child is DisplayObjectContainer) {
                // recursively inspect the child container for textfields
                writeChildTextFieldsToXML(xml, DisplayObjectContainer(child));
            }
        }
    }