查找数组中XML节点的值

时间:2013-03-27 18:25:53

标签: actionscript-3 flex e4x

为什么@assigned总是0?

var gates:XML = <gates>
    <gate_id>
      10991
    </gate_id>
    <gate_id>
      10999
    </gate_id>
    <gate_id>
      11000
    </gate_id>
    <gate_id>
      11001
    </gate_id>
  </gates>;

var assignedGateIds:Array = ["10991","10999"];

for each(var xgate:XML in gates..gate_id){
    var gateId:String = xgate.text();

    if(assignedGateIds.indexOf(xgate.text())>-1){
        xgate.@assigned = 1;
    }else{
        xgate.@assigned = 0;
    }
}

2 个答案:

答案 0 :(得分:0)

简单,因为text()函数不返回字符串而是返回Object。当您使用跟踪时,它会转换为字符串,但如果您使用trace(getQualifiedSuperclassName(xgate.text()));进行检查,则会看到它返回一个对象。

绕过这个我经常使用的 trace(getQualifiedSuperclassName(xgate.text()+''));并将其强制为字符串,您也可以使用as String

var gates:XML = <gates>
    <gate_id>
      10991
    </gate_id>
    <gate_id>
      10999
    </gate_id>
    <gate_id>
      11000
    </gate_id>
    <gate_id>
      11001
    </gate_id>
  </gates>;

var assignedGateIds:Array = ["10991","10999"];

for each(var xgate:XML in gates..gate_id){
    var gateId:String = xgate.text();

    if(assignedGateIds.indexOf(xgate.text()+"")>-1){ // <- added +'' here
        xgate.@assigned = 1;
    }else{
        xgate.@assigned = 0;
    }
}

答案 1 :(得分:0)

更改

    if(assignedGateIds.indexOf(xgate.text())>-1){

    if(assignedGateIds.indexOf(gateId)>-1){

或者

    if(assignedGateIds.indexOf(String(xgate.text()))>-1){

或者

    if(assignedGateIds.indexOf(xgate.text().toString())>-1){