If you are not tied to XML (for example, you have an application server instead of a file server), you might consider using AMF ( Action Message Format ) to transfer data. There are several projects that provide AMF servers, including Adobe Blaze DS and open open community options such as OpenAMF , AMFPHP , PyAMF , etc. This will give you the ability to transfer custom objects from the server to Flex, automatic parsing of data types and type safety. Check out this comparison of data transfer options for an idea of ββthe relative merits.
Thus, XML can be very useful for things like application configuration data, and in cases where you are not using the application server. I agree with another poster where I will immediately parse the xml in the appropriate structures manually. I usually store them using Post-fix VO (for the Value object), and I leave them public. I often do this hierarchically.
package model.vo { public class ConfigVO { public var foo:String; public var bar:int; public var baz:Boolean; public var sections:Array; public function ConfigVO(xml:XML) { parseXML(xml); } private function parseXML(xml:XML):void { foo = xml.foo; bar = xml.bar; baz = (xml.baz == "true"); sections = []; for each(var sectionXML:XML in xml.section) { sections.push(new SectionVO(sectionXML)); } } } } package model.vo { public class SectionVO { public var title:String; public function SectionVO(xml:XML) { parseXML(xml); } private function parseXML(xml:XML):void { title = xml.@title ; } } }
In the past, I have seen systems that associate XML element names with class definitions. Usually they use some kind of introspection in the class to determine which properties to read xml or they will have some properties of a static class that map instance properties to xml tags or attributes. In my experience, they are much more complicated than they are worth it. It takes only 2 seconds to add a new public object and one line to read data from an attribute or child tag. My suggestion is to avoid such complex schemes, but I will give a simple example of completeness.
package model { public class XMLReader { // maps <tagname> to a class private static var elementClassMap:Object = { "section": SectionVO, "person": PersonVO }; public var parsedElements:Array; public function XMLReader(xml:XML) { parsedElements = []; parseXML(xml); } private function parseXML(xml:XML):void { var voClass:Class; for each(var element:XML in xml.children()) { voClass = elementClassMap[element.name().localname]; if(voClass) { parsedElements.push(new voClass(element)); } } } } }
source share