AS3:合并 XML 文件

2024-01-03

这里有很多关于将 XML 与 Java 合并的帖子,但我似乎找不到任何针对同一任务的 Actionscript 参考。

我有一组需要加载的 XML 文件。我希望它们在内存中排序为一个 XML 对象。

例如,假设这些是我的 XML 文件:

File 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>

File 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>

Merged

<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>

在此示例中,两个文件被合并,并且单元被放置在类似的类型名称和组中。

排序优先级:类型名称 > 类型组 > 单位值

我希望这是清楚的。请询问是否需要额外说明。


这应该适用于给定的示例。

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 == t1.@name && @group == t1.@group))
    t1.appendChild(t2.unit.toXMLString());
for each(t2 in file2.type)
  if(merged.type.(@name == t2.@name && @group == t2.@group).length() == 0)
    merged.appendChild(t2.toXMLString());
trace(merged.toXMLString());
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

AS3:合并 XML 文件 的相关文章

随机推荐