2010-10-22 9 views
1

J'ai XML ci-dessousActionScript: comment puis-je obtenir des noeuds XML par attributs

<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"/> 

Maintenant, je dois obtenir toute base de bord sur nodeId,

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

Répondre

3

Essayez ceci: document.edges.(@fromId == "id1"), où document est votre Objet XML. Vous pouvez également parcourir les bords pour trouver ceux dont vous avez besoin:

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]()); 
} 

Sortie:

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

+1 pour votre temps. – alxx

+0

@alxx Eh bien, [mon code compilait] (http://xkcd.com/303/) – Amarghosh

+0

Je veux dire, bon exemple, très clair et complet! – alxx

Questions connexes