2010-01-15 108 views
2

這裏有很多關於將XML與Java合併的帖子,但我似乎無法找到任何對Actionscript的任何參考來完成同一任務。AS3:合併XML文件

我有一組需要加載的XML文件。我希望他們在內存中排序成一個XML對象。

例如,讓我們說這些是我的XML文件:

文件1

<xml> 
    <type name="1" group="a"> 
     <unit> value1 </unit> 
    </type> 
    <type name="1" group="b"> 
     <unit> value2 </unit> 
    </type> 
    <type name="2" group="a"> 
     <unit> value3 </unit> 
    </type> 
</xml> 

文件2

<xml> 
    <type name="1" group="a"> 
     <unit> value4 </unit> 
    </type> 
    <type name="1" group="b"> 
     <unit> value5 </unit> 
    </type> 
    <type name="2" group="a"> 
     <unit> value6 </unit> 
    </type> 
    <type name="3" group="a"> 
     <unit> value7 </unit> 
    </type> 
</xml> 

合併

<xml> 
    <type name="1" group="a"> 
     <unit> value1 </unit> 
     <unit> value4 </unit> 
    </type> 
    <type name="1" group="b"> 
     <unit> value2 </unit> 
     <unit> value5 </unit> 
    </type> 
    <type name="2" group="a"> 
     <unit> value3 </unit> 
     <unit> value6 </unit> 
    </type> 
    <type name="3" group="a"> 
     <unit> value7 </unit> 
    </type> 
</xml> 

在這個例子中,兩個文件被合併,並且單元被放置在相似的類型名稱和組中。

排序優先級:類型名稱>鍵入集團>單位數值

我希望是十分明顯的。請詢問是否需要進一步澄清。

回答

2

這應該適用於給出的示例。

var t1:XML, t2:XML; 
var merged:XML = new XML(file1.toXMLString()) 
for each(t1 in merged.type) 
    for each(t2 in file2.type.(@name == [email protected] && @group == [email protected])) 
    t1.appendChild(t2.unit.toXMLString()); 
for each(t2 in file2.type) 
    if(merged.type.(@name == [email protected] && @group == [email protected]).length() == 0) 
    merged.appendChild(t2.toXMLString()); 
trace(merged.toXMLString());