AS3:合并XML文件 [英] AS3: Merging XML Files

查看:240
本文介绍了AS3:合并XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一些职位就到这里约与Java合并XML的,但我似乎无法找到任何引用的ActionScript相同的任务。

There are a number of posts on here about merging XML with Java, but I can't seem to find any reference to Actionscript for the same task.

我有一组我需要加载XML文件。我希望他们整理到内存中的一个XML对象。

I have a group of XML files that I need to load. I want them to sort into one XML Object in memory.

例如,让我们说这些都是我的XML文件:

For example, let's say these were my XML files:

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

在这个例子中,两个文件被合并,和单位的被放置于像类型名称和组。

In this example, the two files are merged, and unit's are placed within like type names and groups.

排序优先级:的类型名称>键入集团>单位数值

Sorting priority: Type Name > Type Group > Unit Value

我希望是十分明显的。请询问进一步的澄清是必要的。

I hope that's clear. Please ask if additional clarification is necessary.

推荐答案

这应该适用于给出的例子。

This should work for the given example.

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());

这篇关于AS3:合并XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆