i want to use XML to save some configuration for elements in my app. in my example i want to add 6 additional configuration sets to one "main" XML. each set can be config1 or config2. In this case i added 3x config1 and 3x config2. if i trace my results i do not only get the wrong order of elements but also some "strange" binding behavior. Of course this is a simplified example. my configuration sets are more complex (this is why i use seperate xml-objects for each config).
Can someone tell me how this is supposed to work ?
thanks, quadword
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark" applicationDPI="160" creationComplete="init();">
<fx:Declarations>
<fx:XML id="mainConfig" format="e4x">
<allConfigSets>
<viewconfig>Baseconfig</viewconfig>
</allConfigSets>
</fx:XML>
<fx:XML id="configSet1" format="e4x">
<configSet><viewconfig>Set1</viewconfig></configSet>
</fx:XML>
<fx:XML id="configSet2" format="e4x">
<configSet><viewconfig>Set2</viewconfig></configSet>
</fx:XML>
</fx:Declarations>
<fx:Script>
<![CDATA[
private function init(): void {
mainConfig.appendChild(configSet1.viewconfig);
mainConfig.appendChild(configSet1.viewconfig);
mainConfig.appendChild(configSet1.viewconfig);
mainConfig.appendChild(configSet2.viewconfig);
mainConfig.appendChild(configSet2.viewconfig);
mainConfig.appendChild(configSet2.viewconfig);
// trace1 (see below): trace shows wrong order of elements
trace (mainConfig);
// trace2:(see below): changing data on original configSet seems to bind into mainConfig
configSet1.viewconfig = "-";
trace (mainConfig);
}
]]>
</fx:Script>
</s:Application>
Trace1:
<allConfigSets>
<viewconfig>Baseconfig</viewconfig>
<viewconfig>Set1</viewconfig>
<viewconfig>Set2</viewconfig>
<viewconfig>Set2</viewconfig>
<viewconfig>Set2</viewconfig>
<viewconfig>Set1</viewconfig>
<viewconfig>Set1</viewconfig>
</allConfigSets>
Trace2:
<allConfigSets>
<viewconfig>Baseconfig</viewconfig>
<viewconfig>-</viewconfig>
<viewconfig>Set2</viewconfig>
<viewconfig>Set2</viewconfig>
<viewconfig>Set2</viewconfig>
<viewconfig>-</viewconfig>
<viewconfig>-</viewconfig>
</allConfigSets>
Using AS3 XML Objects does not solve this issue:
private function init(): void {
var mainConfig:XML = <allConfigSets><viewconfig>0</viewconfig></allConfigSets>
var configSet1:XML = <configSet><viewconfig>1</viewconfig></configSet>
var configSet2:XML = <configSet><viewconfig>2</viewconfig></configSet>
mainConfig.appendChild(configSet1.viewconfig);
mainConfig.appendChild(configSet1.viewconfig);
mainConfig.appendChild(configSet1.viewconfig);
mainConfig.appendChild(configSet2.viewconfig);
mainConfig.appendChild(configSet2.viewconfig);
mainConfig.appendChild(configSet2.viewconfig);
// trace1 (see below): trace shows wrong order of elements
trace (mainConfig);
// trace2:(see below): changing data on original configSet seems to bind into mainConfig
configSet1.viewconfig = "-";
trace (mainConfig);
}
Trace1:
<allConfigSets>
<viewconfig>0</viewconfig>
<viewconfig>1</viewconfig>
<viewconfig>2</viewconfig>
<viewconfig>2</viewconfig>
<viewconfig>2</viewconfig>
<viewconfig>1</viewconfig>
<viewconfig>1</viewconfig>
</allConfigSets>
Trace2:
<allConfigSets>
<viewconfig>0</viewconfig>
<viewconfig>-</viewconfig>
<viewconfig>2</viewconfig>
<viewconfig>2</viewconfig>
<viewconfig>2</viewconfig>
<viewconfig>-</viewconfig>
<viewconfig>-</viewconfig>
</allConfigSets>
The simultaneous change happens, because you keep appending multiple references to the exact same node (instead of new nodes).
Think of it this way (pseudo code!):
<node1>value</node1>
<node2>
<reference>Go, look at node1!</reference>
<reference>Go, look at node1!</reference>
<reference>Go, look at node1!</reference>
</node2>
Whenever a reference is encountered, the value stored in node1 is returned. When you change that original value, since all the references point to the same node, they will also return the same new value.
To append new copies of your nodes instead of the original, use
mainConfig.appendChild(configSet1.viewconfig.copy());
but keep in mind that copy() returns a copy of the entire subtree of the node, not just the node itself.
As to why the order of elements is wrong, I am really at a loss - XML#appendChild() should add the elements to the end of the list of child nodes. Does trace (mainConfig.toXMLString());
return the same result?