Commit 85d35c6f51e30a2efb2cd7e872e80b22ad741bf8

Authored by Georg Hopp
1 parent efb6ef7a

erste Version von xmlify fuer JavaScript hinzugefügt. Im Moment nur deXmlify...w…

…arscheinlich brache ich auch ein xmlify() nicht.
@@ -4,11 +4,35 @@ require_once dirname(__FILE__) . '/../config.php'; @@ -4,11 +4,35 @@ require_once dirname(__FILE__) . '/../config.php';
4 require_once LIBDIR . 'c_personAdmin.php'; 4 require_once LIBDIR . 'c_personAdmin.php';
5 require_once LIBDIR . 'c_xmlify.php'; 5 require_once LIBDIR . 'c_xmlify.php';
6 6
7 -$personAdmin = new c_personAdmin (); 7 +/*$personAdmin = new c_personAdmin ();
8 $persons = xmlify ($personAdmin->getPersons ()); 8 $persons = xmlify ($personAdmin->getPersons ());
9 $p = deXmlify ($persons); 9 $p = deXmlify ($persons);
  10 +*/
10 11
11 -exit (1); 12 +class dummy extends c_xmlify
  13 +{
  14 + protected $var1;
  15 + protected $var2;
  16 + protected $var3;
  17 +
  18 + function __construct ($xr = NULL, $v1, $v2, $v3)
  19 + {
  20 + if ($xr !== NULL)
  21 + parent::__construct ($xr);
  22 +
  23 + $this->var1 = $v1;
  24 + $this->var2 = $v2;
  25 + $this->var3 = $v3;
  26 + }
  27 +};
  28 +
  29 +$_data = new dummy (
  30 + NULL, array ('var1'=>0.53, 'var2'=>1.1), array (12, 13), "Hallo");
  31 +
  32 +//$_data = array (array ('var1'=>0.53, 'var2'=>1.1), array (12, 13), "Hallo");
  33 +
  34 +$data = xmlify ($_data);
  35 +unset ($_data);
12 36
13 $encoding = FALSE; 37 $encoding = FALSE;
14 if (isset ($_SERVER['HTTP_ACCEPT_ENCODING']) && 38 if (isset ($_SERVER['HTTP_ACCEPT_ENCODING']) &&
@@ -22,7 +46,7 @@ header ('Content-type: text/xml'); @@ -22,7 +46,7 @@ header ('Content-type: text/xml');
22 46
23 $result = FALSE; 47 $result = FALSE;
24 if ($encoding !== FALSE) 48 if ($encoding !== FALSE)
25 - $result = gzcompress ($persons); 49 + $result = gzcompress ($data);
26 50
27 if ($result !== FALSE) 51 if ($result !== FALSE)
28 { 52 {
@@ -32,6 +56,6 @@ if ($result !== FALSE) @@ -32,6 +56,6 @@ if ($result !== FALSE)
32 print ($result); 56 print ($result);
33 } 57 }
34 else 58 else
35 - print ($persons); 59 + print ($data);
36 60
37 ?> 61 ?>
  1 +function extend (subClass, baseClass)
  2 +{
  3 + function inheritance() {}
  4 + inheritance.prototype = baseClass.prototype;
  5 +
  6 + subClass.prototype = new inheritance();
  7 + subClass.prototype.constructor = subClass;
  8 + subClass.baseConstructor = baseClass;
  9 + subClass.superClass = baseClass.prototype;
  10 +}
  11 +
  1 +function xmlToVar (data)
  2 +{
  3 + if (!data.hasChildNodes ())
  4 + return null;
  5 +
  6 + var ret = null;
  7 +
  8 + for (var node in data.childNodes)
  9 + {
  10 + var child = data.childNodes[node];
  11 +
  12 + if (child.nodeType === 3 && child.nodeValue !== null)
  13 + {
  14 + ret = data.childNodes[node].nodeValue;
  15 + break;
  16 + }
  17 + }
  18 +
  19 + switch (data.getAttribute ('type'))
  20 + {
  21 + case 'integer': ret = parseInt (ret, 10); break;
  22 + case 'double': ret = parseFloat (ret);
  23 + }
  24 +
  25 + return ret;
  26 +}
  27 +
  28 +function xmlToObjectArray (data)
  29 +{
  30 + var ret = new Array ();
  31 +
  32 + if (! data.hasChildNodes ())
  33 + return ret;
  34 +
  35 + for (var node in data.childNodes)
  36 + {
  37 + var child = data.childNodes[node];
  38 +
  39 + if (child.nodeType === 1)
  40 + {
  41 + name = child.getAttribute ('name');
  42 +
  43 + switch (child.nodeName)
  44 + {
  45 + case 'variable':
  46 + ret[name] = xmlToVar (child); break;
  47 + case 'array':
  48 + ret[name] = xmlToArray (child); break;
  49 + case 'class':
  50 + var cName = child.getAttribute ('class');
  51 +
  52 + if (typeof (eval (cName)) === 'function')
  53 + ret[name] = eval ("new " + cName + "(child)");
  54 + else
  55 + ret[name] = xmlToObjectArray (child);
  56 + }
  57 + }
  58 + }
  59 +
  60 + return ret;
  61 +}
  62 +
  63 +function xmlToArray (data)
  64 +{
  65 + var ret = new Array ();
  66 +
  67 + if (! data.hasChildNodes ())
  68 + return ret;
  69 +
  70 + for (var node in data.childNodes)
  71 + {
  72 + var child = data.childNodes[node];
  73 +
  74 + if (child.nodeType === 1 && child.nodeName === 'item')
  75 + {
  76 + var key = child.getAttribute ('key');
  77 +
  78 + for (var _node in child.childNodes)
  79 + {
  80 + var _child = child.childNodes[_node];
  81 +
  82 + if (_child.nodeType === 1)
  83 + {
  84 + switch (_child.nodeName)
  85 + {
  86 + case 'variable':
  87 + ret[key] = xmlToVar (_child); break;
  88 + case 'array':
  89 + ret[key] = xmlToArray (_child); break;
  90 + case 'class':
  91 + var cName = child.getAttribute ('class');
  92 +
  93 + if (typeof (eval (cName)) === 'function')
  94 + ret[key] = eval ("new " + cName + "(child)");
  95 + else
  96 + ret[key] = xmlToObjectArray (child);
  97 + }
  98 + }
  99 + }
  100 + }
  101 + }
  102 +
  103 + return ret;
  104 +}
  105 +
  106 +function c_xmlify (data)
  107 +{
  108 + if (! data.hasChildNodes ())
  109 + return;
  110 +
  111 + for (var node in data.childNodes)
  112 + {
  113 + var child = data.childNodes[node];
  114 +
  115 + if (child.nodeType === 1)
  116 + {
  117 + var name = child.getAttribute ('name');
  118 +
  119 + if (typeof (this[name]) !== 'undefined')
  120 + {
  121 + switch (child.nodeName)
  122 + {
  123 + case 'variable':
  124 + this[name] = xmlToVar (child); break;
  125 + case 'array':
  126 + this[name] = xmlToArray (child); break;
  127 + case 'class':
  128 + var cName = child.getAttribute ('class');
  129 +
  130 + if (typeof (eval (cName)) === 'function')
  131 + this[name] = eval ("new " + cName + "(child)");
  132 + else
  133 + this[name] = xmlToObjectArray (child);
  134 + }
  135 + }
  136 + }
  137 + }
  138 +}
  139 +
  140 +function deXmlify (data)
  141 +{
  142 + var ret = null;
  143 + var child = data.firstChild;
  144 +
  145 + switch (child.nodeName)
  146 + {
  147 + case 'variable':
  148 + ret = xmlToVar (child); break;
  149 + case 'array':
  150 + ret = xmlToArray (child); break;
  151 + case 'class':
  152 + var cName = child.getAttribute ('class');
  153 +
  154 + if (typeof (eval (cName)) === 'function')
  155 + ret = eval ("new " + cName + "(child)");
  156 + else
  157 + ret = xmlToObjectArray (child);
  158 + }
  159 +
  160 + return ret;
  161 +}
  1 +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2 + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3 +<html>
  4 + <head>
  5 + <title>Nur zum testen</title>
  6 + <meta http-equiv="content-type" content="text/html;charset=utf-8" />
  7 +
  8 + <script src="js/helper.js" type="text/javascript"></script>
  9 + <script src="js/xmlify.js" type="text/javascript"></script>
  10 +
  11 + <script type="text/javascript">
  12 + //<![CDATA[
  13 + var req = null;
  14 + var data;
  15 +
  16 + function dummy (data)
  17 + {
  18 + this.var1 = null;
  19 + this.var2 = null;
  20 + this.var3 = null;
  21 + this.var4 = null;
  22 +
  23 + dummy.baseConstructor.call (this, data);
  24 + }
  25 + extend (dummy, c_xmlify);
  26 +
  27 + function getData ()
  28 + {
  29 + switch (req.readyState)
  30 + {
  31 + case 4:
  32 + if (req.status != 200)
  33 + alert ("Fehler:" + req.status);
  34 + else
  35 + data = deXmlify (req.responseXML);
  36 + console.dir (data);
  37 + return true;
  38 +
  39 + default:
  40 + return false;
  41 + }
  42 + }
  43 +
  44 + function updData ()
  45 + {
  46 + // erstellen des requests
  47 +
  48 + try
  49 + {
  50 + req = new XMLHttpRequest();
  51 + }
  52 + catch (e)
  53 + {
  54 + try
  55 + {
  56 + req = new ActiveXObject("Msxml2.XMLHTTP");
  57 + }
  58 + catch (e)
  59 + {
  60 + try
  61 + {
  62 + req = new ActiveXObject("Microsoft.XMLHTTP");
  63 + }
  64 + catch (failed)
  65 + {
  66 + req = null;
  67 + }
  68 + }
  69 + }
  70 +
  71 + if (req == null)
  72 + alert("Error creating request object!");
  73 +
  74 + // anfrage erstellen (GET, url ist localhost,
  75 + // request ist asynchron
  76 + var url = 'http://localhost/~georg/bilder/ajax+json/ajax.php';
  77 +
  78 + req.open("GET", url, true);
  79 +
  80 + // Beim abschliessen des request wird diese Funktion ausgeführt
  81 + req.onreadystatechange = getData;
  82 +
  83 + req.setRequestHeader("Content-Type",
  84 + "application/x-www-form-urlencoded");
  85 +
  86 + req.send(null);
  87 + }
  88 + //]]>
  89 + </script>
  90 + </head>
  91 +
  92 + <body>
  93 + <script language="javascript">
  94 + //<![CDATA[
  95 + updData ();
  96 + console.log ('%s', 'jokus');
  97 + //]]>
  98 + </script>
  99 + </body>
  100 +</html>
Please register or login to post a comment