2010-10-22 53 views
1

我有以下的ActionScript:我怎樣才能通過屬性

<node id="id1"/><node id="id2"/>... 
<edge id="eid1" fromId="id1" toId="id2"/> 
<edge id="eid2" fromId="id3" toId="id1"/> 
<edge id="eid3" fromId="id2" toId="id4"/> 

現在我需要得到NODEID所有優勢的基礎XML XML節點,

nodeId = id1 -> eid1, eid2 
nodeId = id2 -> eid1, eid3 
nodeId = id3 -> eid2 
nodeId = id5 -> Null 

回答

3

試試這個:document.edges.(@fromId == "id1"),其中document是你的XML對象。 你也可以遍歷邊緣找到你所需要的:

for each (var edge:XML in document.elements("edge")) 
{ 
    if ([email protected] == "id1") 
    { 
     //do something 
    } 
} 
2
var x:XML = <graph> 
    <node id="id1"/> 
    <node id="id2"/> 
    <node id="id3"/> 
    <node id="id4"/> 
    <node id="id5"/> 
    <edge id="eid1" fromId="id1" toId="id2"/> 
    <edge id="eid2" fromId="id3" toId="id1"/> 
    <edge id="eid3" fromId="id2" toId="id4"/> 
</graph>; 

var nodes:XMLList = x.node; 
for(var i = 0; i < nodes.length(); i++) 
{ 
    var edges = x.edge.(@fromId == nodes[i][email protected] || @toId == nodes[i][email protected]); 
    trace("Node #" + nodes[i][email protected] + " " + edges.length()); 
    for(var j = 0; j < edges.length(); j++) 
    trace(edges[j][email protected]()); 
} 

輸出:

Node #id1 2 
eid1 
eid2 
Node #id2 2 
eid1 
eid3 
Node #id3 1 
eid2 
Node #id4 1 
eid3 
Node #id5 0 
+0

+1您的時間。 – alxx 2010-10-22 11:19:28

+0

@alxx那麼,[我的代碼正在編譯](http://xkcd.com/303/) – Amarghosh 2010-10-22 11:23:13

+0

我的意思是,很好的例子,非常清晰和完整! – alxx 2010-10-22 11:38:28