English 中文(简体)
AS3: 插入XML元素甲型 A. 财产
原标题:AS3: Inserting XML Elements Alphabetically By A Property

因此,Im有小小小问题......

I m 在Im类写作中生成XML文档。 请允许我说,这是从XML开始的:

<base>
    <container id="0">
        <element type="Image" x="0" y"0" />
        <element type="Image" x="100" y"0" />
    <container/>
</base>

我想增加<代码><element>s。 第一种分类顺序是“类型”,然后是“x”,然后是“y”。 因此,如果我添加一个新的“类型的”<代码><element>,请说“Text”,我想在“Image”<element>之后插入案文。

例如:

<base>
    <container id="0">
        <element type="Image" x="0" y"0" />
        <element type="Image" x="100" y"0" />
        <element type="Text" x="200" y"100" />
    <container/>
</base>

基本想法是保持名单的分类,因为我增加了<条码>与代号”;至每一条<条码>与代号;container>。 计算数字足够简单,但我可以直截了当。

这些建议值得赞赏。

我认为的唯一东西是将这些类型带入阿雷拉。 添加“新类型”和“指标”。 情感.。

问题回答

我建议把现有的所有XML要素纳入一个阵列,增加你的新的内容,对阵列进行分类,然后撰写每一件内容。 这比试图说明每个新内容应列入哪一个指数要容易。 这可能不是非常有效的,但取决于你的情况。

为了进行这种分类,我要尝试像这样的东西(未测试):

array.sort(function (a:XML, b:XML) {
    if (a.@type < b.@type) return -1;
    if (a.@type > b.@type) return +1;
    if (Number(a.@x) != Number(b.@x)) return Number(a.@x) - Number(b.@x);
    return Number(a.@y) - Number(b.@y);
});

EDIT:与功能比较的添加物。

您能否改变XML结构? 您可在<代码><images> node中总结所有图像内容。 <代码><texts> node等中的案文内容,这样就很容易在适当地点添加内容。

您仍可使用<代码><element> nodes byaccessing base.container.*.element

检验了......假定你拥有有效的XML,并且你为具有与原XML相同的根子的XML形成一个变量。 地雷只是作为装置。

var sortedXML:XML = <devices></devices>;

function sortBy(xmlFile:XML , field:String, order:int)
{
    // array that will hold index of nodes
    var arr:Array = new Array();
    // loop through nodes
    for each (var someNode:XML in xmlFile.*)
    {
        // array gets populated with node that matches  field 
        arr.push(someNode.child(field));
    }
    // sort the  arr  array alphabetically
    arr.sort();

   // reverse the alphabetical order if necessary
   if (order == -1)
   {
      arr.reverse();
   }

   // append the nodes in from the source, in the order of the array
   for (var i:int=0; i<arr.length; i++)
   {
    sortedXML.appendChild(myXML.device.(child(field) == arr[i]));
   }
}
// trace your original xml then trace sortedXML to verify




相关问题
How do I sort enum members alphabetically in Java?

I have an enum class like the following: public enum Letter { OMEGA_LETTER("Omega"), GAMMA_LETTER("Gamma"), BETA_LETTER("Beta"), ALPHA_LETTER("Alpha"), private final String ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Sorting twodimensional Array in AS3

So, i have a two-dimensional Array of ID s and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array -...

Linq operations against a List of Hashtables?

I m working with a set of legacy DAO code that returns an IList, where each Hashtable represents the row of a dynamically executed SQL query. For example, the List might contain the following records/...

C++ Array Sort Me

Stuck on an array sorter. Have to sort numbers from largest to smallest. I m trying two loops (one nested in the other). Here s the code: int counter=0; // inner counter int counter2=0; // outer ...

Can I Nest OrderBy in .NET?

This doesn t seem to work as I intend. VB.NET: Dim x = Model.Discussions.OrderByDescending(Function(d) d.Messages.OrderByDescending(Function(m) m.Sent).First.Sent) For Each d As Discussion In x ....

sorting elements javascript

I m looking for a way to sort my elements, but it isn t as easy as it sounds. Please let me explain My elements are grouped per 6 elements (thumbnails), each x represents a thumbnail However all ...

热门标签