AS3搜索功能,用于搜索来自XML的2d数组

时间:2011-02-22 16:37:26

标签: xml arrays actionscript-3 search 2d

所以,我有这个任务,我必须在flash中加载Xml文件然后搜索它并在找到它时显示结果。 所以我的xml看起来像这样:                ju675      kyu49     

<item image="John.jpg" name="John" state = "New Jersey" >
<complex fid = "0"> mg749</complex>
<complex fid = "1"> ks749</complex>
<complex fid = "2"> ks678</complex>
    </item>

<item image="Smith.jpg" name="Smith" state = "California">
<complex fid = "0"> we649</complex>
<complex fid = "1"> sd449</complex>
<complex fid = "2"> df459</complex>
<complex fid = "3"> hj569</complex>
    </item>
</content>

我在跟踪复杂问题时遇到了问题。我想要一个搜索状态和复杂的搜索功能。所以我上传XMl

loadXML();
function loadXML():void
{
var loader:URLLoader=new URLLoader();
loader.addEventListener(Event.COMPLETE,completeXMLHandler);

var request:URLRequest=new URLRequest('test4.xml');
try 
{
    loader.load(request);
} 
catch(error:Error) 
{
    trace('Impossible to load the document.');
}
}

 function completeXMLHandler(event:Event):void
 {
var loader:URLLoader=URLLoader(event.target);
var result:XML=new XML(loader.data);
var myXML:XMLDocument=new XMLDocument();
myXML.ignoreWhite=true;
myXML.parseXML(result.toXMLString());

for each (var usr in result.item)

var node:XMLNode=myXML.firstChild;
var items:int=int(node.childNodes.length);
for(var i:int=0;i<items;i++)
{

    var obj:Object=new Object();
    obj.name = node.childNodes[i].attributes.name;
    obj.state = node.childNodes[i].attributes.state;
    obj.image = node.childNodes[i].attributes.image;

    tyu.push(obj);

    var complexes:int=int(node.childNodes[i].childNodes.length);

    for(var j:int=0;j<complexes;j++)
    {

    var ghj:Object=new Object();
    ghj.complex=node.childNodes[i].childNodes[j].firstChild.nodeValue;
    comp.push(ghj);

    }
   main.push(comp);
   trace(main[0][1].complex)
}

currentUserbase = tyu.concat();
}

因此,如果我尝试跟踪“trace(main [1] [1] .complex)”,它会给我一个错误。 然后我使用了搜索功能:

function search(MouseEvent):void
{
currentUserbase = [];

for (var n:int = 0; n<tyu.length; n++){

if ((tyu[n].state.toLowerCase() == 
  search_state.text.toLowerCase() || search_state.text==""))
 {  
 currentUserbase.push(tyu[n]);
 }

 /*for (var k:int = 0; k<comp.length; k++){

if ((comp[n][k].complex.toLowerCase() ==
 autoComplete.text.toLowerCase() ||autoComplete.text==""))
{
    currentUserbase.push(comp[n][k]);
}
}*/



}
 updateList();
}

伙计们,我正试图从几天开始就把它弄好。请帮帮我。如果你想看到.fla文件,请告诉我。

2 个答案:

答案 0 :(得分:0)

正如@Cay建议的那样,看一下E4X,它会节省你一些时间,否则就花在转换为对象和遍历它们上。

代码可以做一些简化:

loadXML();
function loadXML():void {
    var loader:URLLoader=new URLLoader(new URLRequest('test4.xml'));//passing the url loads it automatically
    loader.addEventListener(Event.COMPLETE,completeXMLHandler);
    loader.addEventListener(IOErrorEvent.IO_ERROR, errorXMLHandler);
}
function errorXMLHandler(event:Event):void {
    trace('Impossible to load the document at url: ' + event.target.url);
}
function completeXMLHandler(event:Event):void {
    var result:XML=new XML(event.target.data);
    var items:XMLList = result.item;
    var complexes:XMLList = items.complex;
    var secondCompexItemsOnly:XMLList = items.complex.(@fid == "2");
    trace(items);
    trace(complexes);
    trace(secondCompexItemsOnly);
}

稍微看一下similar question

更新: @hanna,我仔细阅读了你的评论,但我仍然不能100%肯定你想要达到的目标。我猜你想要找到具有正确状态的节点(从状态输入中选取),以及正确的复数id(节点值)。我假设你可以有多个具有正确状态的项目节点,但名称不同,可能包含具有正确复杂节点值的复杂节点。

这就是我的意思:

var myXML:XML = <content><item image="John.jpg" name="John" state = "New Jersey" >
<complex fid = "0"> mg749</complex>
<complex fid = "1"> ks749</complex>
<complex fid = "2"> ks678</complex>
    </item>

<item image="Smith.jpg" name="Smith" state = "California">
<complex fid = "0"> we649</complex>
<complex fid = "1"> sd449</complex>
<complex fid = "2"> df459</complex>
<complex fid = "3"> hj569</complex>
    </item>
</content>;

function getItemByStateAndID(state:String,id:String,source:XML):XML{
    var resultsByState:XMLList = source.item.(@state == state);
    if(resultsByState){//if there are states with the required name, look for complex nodes with requred id
        var result:XML = <content />;
        for each(var item:XML in resultsByState){//might be multiple items with the same state, but different content ?
            var resultById:XMLList = item.complex.(text() == id);
            if(resultById){//if there are complex nodes with the right id
                var resultItem:XML = item.copy();//copy the item node, so we preserve it's attributes(image,name,state)
                     resultItem.complex = resultById;//replace the complex node list with the result;
                result.appendChild(resultItem);//add it to our main result node, might be multiple item nodes with same states and different names, but the right id, b
            }   
        }
        return result;//got all item nodes with the right state and ids, return values;     
    }
    return null;//no results, return null   
}

trace(getItemByStateAndID("New Jersey","ks678",myXML));
/*outputs: <content>
<item image="John.jpg" name="John" state="New Jersey">
  <complex fid="2">ks678</complex>
</item>
</content>*/

HTH

答案 1 :(得分:0)

http://kirill-poletaev.blogspot.com/2011/01/search-through-xml-database-using-as3_12.html
我有这样的东西,但只有两个搜索字段,比如说ex在这里,两个搜索字段是name和Job。此示例的Xml如下:

<userbase>
<user id="1" firstname="John" secondname="McCallister"
 sex="Male" age="26"   job="Driver" />
<user id="2" firstname="Adam" secondname="Jackson" sex="Male" 
age="25" job="Shop assistant" />
<user id="3" firstname="Mary" secondname="McCallister" sex="Female" 
age="24" job="Manager" />
</userbase>

所以上面的一个正在工作,但我想要的是使用以下XML来处理这个例子。

userbase>
<user id="1" firstname="John" secondname="McCallister" sex="Male" age="29">
<job>driver</job>
<job>mechanic</job>
<job>guide</job>
</user>

<user id="2" firstname="Adam" secondname="Jackson" sex="Male" age="25">
<job>Teacher</job>
<job>Shop assistant</job>
</user>

<user id="3" firstname="Mary" secondname="McCallister" sex="Female" age="24">    
<job>Manager</job>
<job>doctor</job>
<job>shopkeeper</job>
<job>electrician</job>
</user>
</userbase>

我想在列表中显示名字和名字。为名字和工作创建搜索功能。按下搜索时,它显示作业关联的第一个和第二个名称。由于有不规则数量的作业节点,我不知道如何执行它。