Created
November 5, 2010 22:18
-
-
Save dannykopping/664962 to your computer and use it in GitHub Desktop.
Deserialize XML to an Object instance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private function xmlToObject(xml:XML, chain:Object=null):Object | |
{ | |
if(!chain) | |
chain = {}; | |
for each(var child:XML in xml.children()) | |
{ | |
(child.children().length() > 1 || (child.children().length() == 1 && child.hasComplexContent())) | |
? chain[child.name().toString()] = xmlToObject(child, chain[child.name().toString()]) | |
: chain[child.name().toString()] = chain[child.name().toString()] = child.text().toString(); | |
} | |
return chain; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var xml:XML = <data> | |
<this> | |
<is>an example</is> | |
<of>a hierarchical</of> | |
<structure> | |
<that> | |
<goes>many levels deep</goes> | |
</that> | |
</structure> | |
</this> | |
</data>; | |
trace(ObjectUtil.toString(xmlToObject(xml))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment