Given the following xml:
<form>
<personalDetails>
<name>John</name>
</personalDetails>
<financeDetails>
<income>
<salary>1000000</salary>
</income>
</financeDetails>
</form>
I know that is it possible to create the above xml as follows (which is very cool):
var xml:XML = <form />;
xml.personalDetails.name = "John";
xml.financeDetails.income.salary = 1000000;
However, what if we do not know the names of the nodes or how many levels exist? We can accomplish this using the method below but it really feels like there should be an easier, better way to do this:
var xml:XML = <form />;
updateXml(xml, "personalDetails.name", "John");
updateXml(xml, "financeDetails.income.salary", "1000000");
function updateXml(xml:XML, path:String, data:String):void {
var nodesArray:Array = path.split(".");
switch (nodesArray.length) {
case 1:
xml[nodesArray[0]] = data;
break;
case 2:
xml[nodesArray[0]][nodesArray[1]] = data;
break;
case 3:
xml[nodesArray[0]][nodesArray[1]][nodesArray[2]] = data;
break;
}
}