Skip to content

Instantly share code, notes, and snippets.

@dannykopping
Created November 5, 2010 22:18
Show Gist options
  • Save dannykopping/664962 to your computer and use it in GitHub Desktop.
Save dannykopping/664962 to your computer and use it in GitHub Desktop.
Deserialize XML to an Object instance
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;
}
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