Haxe迭代定义变量

时间:2015-07-17 22:34:40

标签: xml iteration haxe

我有一大块代码将xml解析为变量。在其中一个变量上,永远不会超过12个,但可能会少一些。我试图每秒多次定义这些变量中的每一个。但是我只需要定义它们,如果它们有变量,因为现在,如果少于12个变量,程序崩溃。现在这个代码可以工作,但前提是我的xml文档中有12个值。如何删除此代码,以便我没有#34; if!= null"声明?

var _vol1 = (ipts[0].volume);
var _vol2 = (ipts[1].volume);
....
....
var _vol12 = (ipts[11].volume);

当我的xml文档中包含12个值时,此代码有效,但如果少于12个值,则会崩溃,有时可能会出现这种情况。

如果有值并且不为null,我如何构造代码来定义/重新定义变量?这需要什么迭代????

If ((ipts[0].volume) != null ){
    var _vol1 = (ipts[0].volume);
}

我在Haxe网站上进行过研究,但不知道这在哪里适用。我的语法和编程并不是最好的。谢谢您的帮助。对不起,如果这是一个糟糕的问题。

更新:这是我现在的完整代码。它现在没有崩溃,但是现在我添加了if语句

,它不再定义卷变量了
var xml = Xml.parse(_vMixData);

// wrap the xml for fast access
var data = new haxe.xml.Fast(xml.firstElement());

//Getting the data from inputs (here we are getting the xml node 'inputs', which contain two 'input' nodes, as per your sample xml file.
var inputs = data.node.inputs;
var ipts = new Array<VInput>();

for (input in inputs.nodes.input) {
    var ipt = new VInput();
    if (input.has.state) { 
       ipt.state = input.att.state;
       ipt.value = input.innerHTML; 
           }
    if (input.has.volume) {
        ipt.volume = Std.parseInt(input.att.volume);
          }
    if (input.has.muted) {
        ipt.muted = Std.string(input.att.muted);
          }
    ipt.value = input.innerHTML;          
    ipts.push(ipt);
}

var overlays = data.node.overlays;
var ovlys = new Array<VOverlay>();
for (overlay in overlays.nodes.overlay) {
    var ovly = new VOverlay();
    if (overlay.has.number) { 
        ovly.number = Std.parseInt(overlay.att.number);
    }
    ovly.value = overlay.innerHTML;
    ovlys.push(ovly);
}

//Defines variables based on returned information from vMix
var _fadeToBlack:Bool = data.node.fadeToBlack.innerHTML == "True" ? true : false;

var _version = data.node.version.innerHTML;
var _record:Bool = data.node.recording.innerHTML == "True" ? true : false;
var _external:Bool = data.node.external.innerHTML == "True" ? true : false;
var _stream:Bool = data.node.streaming.innerHTML == "True" ? true : false;
var _active:Int = Std.parseInt(data.node.active.innerHTML);
var _preview:Int = Std.parseInt(data.node.preview.innerHTML);

var _overlay1 = (ovlys[0].value);
var _overlay2 = (ovlys[1].value);
var _overlay3 = (ovlys[2].value);
var _overlay4 = (ovlys[3].value);
var _input1state = (ipts[0].state);


if (ipts[0].volume != null ){
var _vol1 = (ipts[0].volume);
}
if (ipts[1].volume != null ){
var _vol2 = (ipts[1].volume);
}
if (ipts[2].volume != null ){
var _vol3 = (ipts[2].volume);
}
if (ipts[3].volume != null ){
var _vol4 = (ipts[3].volume);
}
if (ipts[4].volume != null ){
var _vol5 = (ipts[4].volume);
}
if (ipts[5].volume != null ){
var _vol6 = (ipts[5].volume);
}
if (ipts[6].volume != null ){
var _vol7 = (ipts[6].volume);
}
if (ipts[7].volume != null ){
var _vol8 = (ipts[7].volume);
}
if (ipts[8].volume != null ){
var _vol9 = (ipts[8].volume);
}
if (ipts[9].volume != null ){
var _vol10 = (ipts[9].volume);
}
if (ipts[10].volume != null ){
var _vol11 = (ipts[10].volume);
}
if (ipts[11].volume != null ){
var _vol12 = (ipts[11].volume);
}

//var _1mute = (ipts[0].muted);
//var _2mute = (ipts[1].muted);
//var _3mute = (ipts[2].muted);
//var _4mute = (ipts[3].muted);
//var _5mute = (ipts[4].muted);
//var _6mute = (ipts[5].muted);
//var _7mute = (ipts[6].muted);
//var _8mute = (ipts[7].muted);
//var _9mute = (ipts[8].muted);
//var _10mute = (ipts[9].muted);
//var _11mute = (ipts[10].muted);
//var _12mute = (ipts[11].muted);



if (ipts[2] != null){
    var _ovly3 = 1;
}
//trace(ovlys[2].value);
//trace(ovlys[0].value);
//trace(ipts[0].state);
//trace(_active);
//trace(_preview);
//trace(_fadeToBlack);

1 个答案:

答案 0 :(得分:1)

要查看XML中的每个内容,请执行for (child in xml)。要拥有任意数量的变量,您需要使用数组,而不是var1到varInfinity。

你没有发布足够的代码,所以我猜这里:

var varArray = [];
for (child in xml) {
    varArray.push(child.volume);
}

这样,无论你有多少变量,代码都适应(而不是通过添加var1,var2等来适应)。