Archive for the ‘XML/YAML’ Category
Generate UI from XML, and re-save modified updates
Alright, so we’ll be dynamically generating a UI interface from an XML. So I’ll show you the XML first:
<mx:XML id="xml_save">
<cfg>
<checkbox id="ebay" enabled="true" description="eBay"/>
<checkbox id="wikipedia" enabled="false" description="wikipedia"/>
<checkbox id="pandora" enabled="true" description="pandora"/>
<checkbox id="flexr" enabled="false" description="flexr"/>
</cfg>
</mx:XML>
Now for each of the shortcut node in the XML above, we’re going to create a checkbox for each of them, style it, then see whether its checked or not (and display it), and then add it to a VBox called ‘p_CheckContainer‘.
private function updateForm(xml_result:XML):void {
for each (var node:Object in xml_result.checkbox) {
var checkBox:CheckBox = new CheckBox(); // new checkbox for each node
checkBox.setStyle('paddingLeft', 30); // style it
checkBox.label = node.@description; // add it's label
checkBox.selected = TypeConverter.parseBoolean*(node.@enabled); // un/checked
checkBox.id = node.@id; // identify it
p_CheckContainer.addChild(checkBox); // display it
}
}
* TypeConverter is a class with static public method parseBoolean which returns a true/false boolean.
——
Now, After we generated the UI from the XML, we want to save any changes, say for example, the user un/checked one of these checkboxes, and we want to resave the new XML with the appropriate values:
private function saveChanges():void {
var _numCheckboxes:Number = p_CheckContainer.numChildren; // Number of CheckBoxes
m_modifiedXML = <cfg></cfg>; // member var of type XML
for (var i:Number= 0; i <= _numCheckboxes - 1; i++) {
var _checkBox:CheckBox = p_CheckContainer.getChildAt(i) as CheckBox;
var _newNode:XML = <shortcut/>;
_newNode.@id= _checkBox.id;
_newNode.@enabled = _checkBox.selected;
m_modifiedXML.appendChild(_newNode);
}
trace ("m_modifiedXML: " + m_modifiedXML);
}