ActionScript:如何通过属性获取xml节点

时间:2010-10-22 07:35:11

标签: xml flex actionscript-3

我在下面有XML

<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上获得所有优势,

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

2 个答案:

答案 0 :(得分:3)

试试这个:document.edges.(@fromId == "id1"),其中document是您的XML对象。 您也可以遍历边缘以找到所需的边缘:

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

答案 1 :(得分: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].@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());
}

输出:

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