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

时间:2022-02-06 07:19:05

I have XML below

我有下面的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"/>

Now I need get all edge base on nodeId,

现在我需要在nodeId上建立所有的边缘,

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

2 个解决方案

#1


3  

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

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

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

#2


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

Output:

输出:

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

#1


3  

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

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

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

#2


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

Output:

输出:

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