开发者

ActionScript: how can I get xml nodes by attributes

I have XML below

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

Now I need get all edge base on nodeId,

nodeId = id1  -> eid1, eid2
nodeId = id2开发者_如何学编程  -> eid1, eid3
nodeId = id3  -> eid2
nodeId = id5  -> Null


Try this: document.edges.(@fromId == "id1"), where document is your XML object. You may also iterate through edges to find ones you need:

for each (var edge:XML in document.elements("edge"))
{
    if (edge.@fromId == "id1")
    {
        //do something
    }
}


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].@id || @toId == nodes[i].@id);
  trace("Node #" + nodes[i].@id + " " + edges.length());
  for(var j = 0; j < edges.length(); j++)
     trace(edges[j].@id.toString());
}

Output:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜