I am trying to write a code in in Haxe in a program called Stencyl. I am trying to take all of the date in a return XML response from a web server and assign variables to them. I have gotten the basic XML response and was able to use the Fast XML call but do not know how to define all the data into variables to be used in the program. The number of tags may change from call to call. Here is what I have so far but do not know how to parse the whole document into variables. Below is also sample XML data. Any help would be awesome!
// parse some xml data
var xml = Xml.parse(_vMixData).firstElement();
// wrap the xml for fast access
var fast = new haxe.xml.Fast(xml.firstElement());
// access the "inputs" child, which is wrapped with haxe.xml.Fast too
var inputs = fast.node.inputs;
Some Sample XML Code
<vmix>
<version>14.0.0.52</version>
<inputs>
<input key="7715f2db-bdfd-4a7b-ab50-206dd26411cf" number="1" type="Video" title="Dord..mp4" state="Paused" position="0" duration="776214" loop="False" muted="False" volume="100" solo="False" audiobusses="M">Dord..mp4</input><input key="e5362e83-84e3-4b12-84c0-c18dad12570d" number="2" type="Blank" title="Blank" state="Paused" position="0" duration="0" loop="False">Blank</input>
</inputs>
<overlays>
<overlay number="1">Input.mp4</overlay>
<overlay number="2" />
<overlay number="3" />
<overlay number="4" />
<overlay number="5" />
<overlay number="6" />
</overlays>
<preview>2</preview>
<active>1</active>
<fadeToBlack>False</fadeToBlack>
<transitions>
<transition number="1" effect="Zoom" duration="500" />
<transition number="2" effect="Wipe" duration="500" />
<transition number="3" effect="Fly" duration="500" />
<transition number="4" effect="Zoom" duration="1000" />
</transitions>
<recording>False</recording>
<external>False</external>
<streaming>False</streaming>
<playList>False</playList>
<multiCorder>False</multiCorder>
<audio>
<master volume="100" muted="False" headphonesVolume="100" />
</audio>
</vmix>
Here is the code in its entirety and it now doesnt print vMixData where it used to before I changed it.
{
public var _Prog1:Actor;
public var _vMixData:String;
public var _inputvar:String;
public function new(dummy:Int, engine:Engine)
{
super(engine);
nameMap.set("Prog 1", "_Prog1");
nameMap.set("vMixData", "_vMixData");
_vMixData = "";
nameMap.set("inputvar", "_inputvar");
_inputvar = "";
}
override public function init()
{
/* ======================= Every N seconds ======================== */
runPeriodically(1000 * 1, function(timeTask:TimedTask):Void
{
if (wrapper.enabled)
{
visitURL("http://127.0.0.1:8088/api?/Function=", function(event:Event):Void
{
_vMixData = cast(event.target, URLLoader).data;
propertyChanged("_vMixData", _vMixData);
});
var xml = Xml.parse(_vMixData);
// wrap the xml for fast access
var fast = new haxe.xml.Fast(xml.firstElement());
// access the "inputs" child, which is wrapped with haxe.xml.Fast too
var input = fast.node.input;
for (input in fast.node.input)
{
//Checking for and accessing attributes.
if (input.has.key)
trace("Input key : " + input.att.key);
//Accessing contents of a node
trace("Contents of input node : " + input.innerHTML);
}
trace("" + _vMixData);
}
}, null);
}
override public function forwardMessage(msg:String)
{}}
You can use this to loop through nodes:
To loop through all nodes of a specific name you use 'nodes' instead of 'node'. Using it like this would return the first node of the specified name:
EDIT: Alright, let me explain this a little better. To loop through your nodes, and gather your data, you should do this:
For long XML files such as the one you posted, it makes more sense to create a custom object which will receive the values from nodes and attributes, this will make your code more organized. I used local vars like these just to better illustrate the basic usage. Hope this helps you understand the sintax a little better.
To illustrate the custom object creation, here is what the Input class could look like (this is as basic as it gets):
For more information regarding Haxe language in general, i'd recommend consulting the manual:
Haxe manual
There is also this Fast usage reference from the old Haxe site:
Using haxe.xml.Fast
Regards,