从 AS3 中的 XML 中删除重复的 enteries/items
Remove duplicate enteries/items from XML in AS3
我从 XML 文件中得到了重复的结果。我想删除多余的但无法使用 splice
和 indexOf
实现。有人能给我指出正确的方向吗??
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>
这是一个关于如何做你想做的事的例子:
使用这个测试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
我从 XML 文件中得到了重复的结果。我想删除多余的但无法使用 splice
和 indexOf
实现。有人能给我指出正确的方向吗??
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>
这是一个关于如何做你想做的事的例子:
使用这个测试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