【问题标题】:Remove duplicate enteries/items from XML in AS3从 AS3 中的 XML 中删除重复的条目/项目
【发布时间】:2015-07-09 01:27:52
【问题描述】:

我从 XML 文件中得到重复的结果。我想删除多余的,但在使用spliceindexOf 时遇到了麻烦。有人能指出我正确的方向吗??

var xmlLoader:URLLoader = new URLLoader();
var xmlReq:URLRequest = new URLRequest("data.xml");

xmlLoader.load(xmlReq); 

var background:bkg;  var textvar:TextField = new TextField;         
xmlLoader.addEventListener(Event.COMPLETE, convertdata);

function convertdata(event:Event){  
    var xmlinfo:XML = new XML(event.target.data);   
    //trace(xmlinfo);


    var list:XMLList = xmlinfo.profile.photography;

    var totalimage:Number = list.length();

    trace("length " + totalimage);

    enterbtn.addEventListener(MouseEvent.CLICK, entersite);

    function entersite(event:MouseEvent){
        for (var i:int =0; i<totalimage; i++){
            trace(xmlinfo.profile.photography[i]);

            background = new bkg();
            background.y = i*40;
            background.x =80;
            addChild(background);

            textvar = new TextField();
            textvar.text = list[i];    
            background.addChild(textvar);
        }

    }   
}

XML 文件

        <profile>
            <first_name>ann</first_name>
            <last_name> lee</last_name>
            <photography>sport</photography>
            <photography>landscape</photography>
            <photography>still life</photography>           
            <image>img1.jpg</image>

        </profile>

        <profile>   
            <first_name>john</first_name>
            <last_name> thomas</last_name>
            <photography>wildlife</photography>
            <photography>landscape</photography>
            <image>img2.jpg</image>
        </profile>

【问题讨论】:

  • 你如何确定它是否是重复的?你的拼接和 indexOf 代码在哪里?无论如何,您使用delete 关键字,而不是拼接。显示你的相关代码,我可以给你一个完整的例子/答案
  • 这只是 XML 文件的两个示例。该代码返回 9 个值,其中一些是重复的。
  • 嘿 LDMS 我刚刚开始让您的代码为我工作。我在 TypeError: Error #1006: for line var totalimage:Number = list.length(); 中确实有一个问题list.length 现在变成了什么?我尝试了各种解决方案,但都失败了。
  • 把括号去掉就行了。 list.length
  • 当然了。当我尝试这样做时有点晚了。感谢一百万您的帮助。非常感谢。

标签: xml actionscript-3 flash actionscript


【解决方案1】:

这是一个关于如何做你想做的事的例子:

使用此测试 XML

var xml:XML =   <data>
                <profile>
                    <first_name>ann</first_name>
                    <last_name> lee</last_name>
                    <photography>sport</photography>
                    <photography>landscape</photography>
                    <photography>still life</photography>           
                    <image>img1.jpg</image>
                </profile>;
                <profile>   
                    <first_name>john</first_name>
                    <last_name> thomas</last_name>
                    <photography>wildlife</photography>
                    <photography>landscape</photography>
                    <image>img2.jpg</image>
                </profile></data>;

//create an array, and popluate it with your xml nodes
var list:Array = new Array();
xml.profile.photography.(list.push(toString())); 
// ^ this converts your xmlList to array by running the method in brackets on each item in the xmlList

//sort it so duplicate values are grouped together
list.sort();

trace(list); //this traces out your full list

//now iterate that array/vector
var i:int = 0;
while(i < list.length) {

    //while the next item matches the current, splice it out
    while(i < list.length+1 && list[i] == list[i+1]) {
        list.splice(i, 1);
    }
    i++;
}

trace(list); //this will be your list with no duplicates

【讨论】:

  • 太棒了。非常感谢 LDMS。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-30
  • 1970-01-01
  • 1970-01-01
  • 2013-09-17
  • 1970-01-01
  • 2014-08-01
相关资源
最近更新 更多