【发布时间】:2015-07-17 22:34:40
【问题描述】:
我有一大段代码将 xml 解析为变量。在其中一个变量上,永远不会超过 12,但可能会更少。我试图每秒多次定义这些变量中的每一个。但是,如果它们有变量,我只需要定义它们,因为现在,如果变量少于 12 个,程序就会崩溃。现在下面的代码有效,但前提是我的 xml 文档中存在 12 个值。我如何迭代这段代码,所以我没有“if!= null”语句的列表?
var _vol1 = (ipts[0].volume);
var _vol2 = (ipts[1].volume);
....
....
var _vol12 = (ipts[11].volume);
当我的 xml 文档中有 12 个值时,此代码有效,但如果少于 12 个,则有时会崩溃。
只有在有值且不为空的情况下,我如何构造代码以定义/重新定义变量?这有什么需要迭代的???
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);
【问题讨论】: