xmlify.js 3.3 KB
if (typeof (nBildertool) === 'undefined')
	nBildertool = {};

nBildertool.xmlToVar = function (data)
{
	if (!data.hasChildNodes ())
		return null;

	var ret = null;

	for (var node in data.childNodes)
	{
		var child = data.childNodes[node];

		if (child.nodeType === 3 && child.nodeValue !== null)
		{
			ret = data.childNodes[node].nodeValue;
			break;
		}
	}

	switch (data.getAttribute ('type'))
	{
		case 'integer': ret = parseInt (ret, 10); break;
		case 'double':  ret = parseFloat (ret);
	}

	return ret;
}

nBildertool.xmlToObjectArray = function (data)
{
	var ret = new Array ();

	if (! data.hasChildNodes ())
		return ret;

	for (var node in data.childNodes)
	{
		var child = data.childNodes[node];

		if (child.nodeType === 1)
		{
			name = child.getAttribute ('name');

			switch (child.nodeName)
			{
				case 'variable':
					ret[name] = nBildertool.xmlToVar (child); break;
				case 'array':
					ret[name] = nBildertool.xmlToArray (child); break;
				case 'class':
					var cName = child.getAttribute ('class');

					if (eval ('typeof (' + cName + ') === \'function\''))
						ret[name] = eval ("new " + cName + "(child)");
					else
						ret[name] = nBildertool.xmlToObjectArray (child);
			}
		}
	}

	return ret;
}

nBildertool.xmlToArray = function (data)
{
	var ret = new Array ();

	if (! data.hasChildNodes ())
		return ret;

	for (var node in data.childNodes)
	{
		var child = data.childNodes[node];

		if (child.nodeType === 1 && child.nodeName === 'item')
		{
			var key = child.getAttribute ('key');

			for (var _node in child.childNodes)
			{
				var _child = child.childNodes[_node];

				if (_child.nodeType === 1)
				{
					switch (_child.nodeName)
					{
						case 'variable':
							ret[key] = nBildertool.xmlToVar (_child); break;
						case 'array':
							ret[key] = nBildertool.xmlToArray (_child); break;
						case 'class':
							var cName = child.getAttribute ('class');

							if (eval ('typeof (' + cName + ') === \'function\''))
								ret[key] = eval ("new " + cName + "(_child)");
							else
								ret[key] = nBildertool.xmlToObjectArray (_child);
					}
				}
			}
		}
	}

	return ret;
}

nBildertool.c_xmlify = function (data)
{
	if (! data.hasChildNodes ())
		return;

	for (var node in data.childNodes)
	{
		var child = data.childNodes[node];

		if (child.nodeType === 1)
		{
			var name = child.getAttribute ('name');

			if (typeof (this[name]) !== 'undefined')
			{
				switch (child.nodeName)
				{
					case 'variable':
						this[name] = nBildertool.xmlToVar (child); break;
					case 'array':
						this[name] = nBildertool.xmlToArray (child); break;
					case 'class':
						var cName = child.getAttribute ('class');

						if (eval ('typeof (' + cName + ') === \'function\''))
							this[name] = eval ("new " + cName + "(child)");
						else
							this[name] = nBildertool.xmlToObjectArray (child);
				}
			}
		}
	}
}

nBildertool.deXmlify = function (data)
{
	var ret = null;
	var child = data.firstChild;

	switch (child.nodeName)
	{
		case 'variable':
			ret = nBildertool.xmlToVar (child); break;
		case 'array':
			ret = nBildertool.xmlToArray (child); break;
		case 'class':
			var cName = child.getAttribute ('class');

			if (eval ('typeof (' + cName + ') === \'function\''))
				ret = eval ("new " + cName + "(child)");
			else
				ret = nBildertool.xmlToObjectArray (child);
	}

	return ret;
}