From 85d35c6f51e30a2efb2cd7e872e80b22ad741bf8 Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Thu, 25 Oct 2007 09:48:58 +0000 Subject: [PATCH] =?UTF-8?q?erste=20Version=20von=20xmlify=20fuer=20JavaScr?= =?UTF-8?q?ipt=20hinzugef=C3=BCgt.=20Im=20Moment=20nur=20deXmlify...warsch?= =?UTF-8?q?einlich=20brache=20ich=20auch=20ein=20xmlify()=20nicht.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ajax+json/ajax.php | 32 +++++++-- js/helper.js | 11 ++++ js/xmlify.js | 161 +++++++++++++++++++++++++++++++++++++++++++++ testxmlify.html | 100 ++++++++++++++++++++++++++++ 4 files changed, 300 insertions(+), 4 deletions(-) create mode 100644 js/helper.js create mode 100644 js/xmlify.js create mode 100644 testxmlify.html diff --git a/ajax+json/ajax.php b/ajax+json/ajax.php index 6ba91ed..53e13c3 100644 --- a/ajax+json/ajax.php +++ b/ajax+json/ajax.php @@ -4,11 +4,35 @@ require_once dirname(__FILE__) . '/../config.php'; require_once LIBDIR . 'c_personAdmin.php'; require_once LIBDIR . 'c_xmlify.php'; -$personAdmin = new c_personAdmin (); +/*$personAdmin = new c_personAdmin (); $persons = xmlify ($personAdmin->getPersons ()); $p = deXmlify ($persons); +*/ -exit (1); +class dummy extends c_xmlify +{ + protected $var1; + protected $var2; + protected $var3; + + function __construct ($xr = NULL, $v1, $v2, $v3) + { + if ($xr !== NULL) + parent::__construct ($xr); + + $this->var1 = $v1; + $this->var2 = $v2; + $this->var3 = $v3; + } +}; + +$_data = new dummy ( + NULL, array ('var1'=>0.53, 'var2'=>1.1), array (12, 13), "Hallo"); + +//$_data = array (array ('var1'=>0.53, 'var2'=>1.1), array (12, 13), "Hallo"); + +$data = xmlify ($_data); +unset ($_data); $encoding = FALSE; if (isset ($_SERVER['HTTP_ACCEPT_ENCODING']) && @@ -22,7 +46,7 @@ header ('Content-type: text/xml'); $result = FALSE; if ($encoding !== FALSE) - $result = gzcompress ($persons); + $result = gzcompress ($data); if ($result !== FALSE) { @@ -32,6 +56,6 @@ if ($result !== FALSE) print ($result); } else - print ($persons); + print ($data); ?> diff --git a/js/helper.js b/js/helper.js new file mode 100644 index 0000000..19ac7ba --- /dev/null +++ b/js/helper.js @@ -0,0 +1,11 @@ +function extend (subClass, baseClass) +{ + function inheritance() {} + inheritance.prototype = baseClass.prototype; + + subClass.prototype = new inheritance(); + subClass.prototype.constructor = subClass; + subClass.baseConstructor = baseClass; + subClass.superClass = baseClass.prototype; +} + diff --git a/js/xmlify.js b/js/xmlify.js new file mode 100644 index 0000000..31fd4e3 --- /dev/null +++ b/js/xmlify.js @@ -0,0 +1,161 @@ +function xmlToVar (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; +} + +function xmlToObjectArray (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] = xmlToVar (child); break; + case 'array': + ret[name] = xmlToArray (child); break; + case 'class': + var cName = child.getAttribute ('class'); + + if (typeof (eval (cName)) === 'function') + ret[name] = eval ("new " + cName + "(child)"); + else + ret[name] = xmlToObjectArray (child); + } + } + } + + return ret; +} + +function xmlToArray (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] = xmlToVar (_child); break; + case 'array': + ret[key] = xmlToArray (_child); break; + case 'class': + var cName = child.getAttribute ('class'); + + if (typeof (eval (cName)) === 'function') + ret[key] = eval ("new " + cName + "(child)"); + else + ret[key] = xmlToObjectArray (child); + } + } + } + } + } + + return ret; +} + +function c_xmlify (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] = xmlToVar (child); break; + case 'array': + this[name] = xmlToArray (child); break; + case 'class': + var cName = child.getAttribute ('class'); + + if (typeof (eval (cName)) === 'function') + this[name] = eval ("new " + cName + "(child)"); + else + this[name] = xmlToObjectArray (child); + } + } + } + } +} + +function deXmlify (data) +{ + var ret = null; + var child = data.firstChild; + + switch (child.nodeName) + { + case 'variable': + ret = xmlToVar (child); break; + case 'array': + ret = xmlToArray (child); break; + case 'class': + var cName = child.getAttribute ('class'); + + if (typeof (eval (cName)) === 'function') + ret = eval ("new " + cName + "(child)"); + else + ret = xmlToObjectArray (child); + } + + return ret; +} diff --git a/testxmlify.html b/testxmlify.html new file mode 100644 index 0000000..3973d03 --- /dev/null +++ b/testxmlify.html @@ -0,0 +1,100 @@ + + + + Nur zum testen + + + + + + + + + + + +