Browse Source

erste Version von xmlify fuer JavaScript hinzugefügt. Im Moment nur deXmlify...warscheinlich brache ich auch ein xmlify() nicht.

master
Georg Hopp 18 years ago
parent
commit
85d35c6f51
  1. 32
      ajax+json/ajax.php
  2. 11
      js/helper.js
  3. 161
      js/xmlify.js
  4. 100
      testxmlify.html

32
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);
?>

11
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;
}

161
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;
}

100
testxmlify.html

@ -0,0 +1,100 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Nur zum testen</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<script src="js/helper.js" type="text/javascript"></script>
<script src="js/xmlify.js" type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
var req = null;
var data;
function dummy (data)
{
this.var1 = null;
this.var2 = null;
this.var3 = null;
this.var4 = null;
dummy.baseConstructor.call (this, data);
}
extend (dummy, c_xmlify);
function getData ()
{
switch (req.readyState)
{
case 4:
if (req.status != 200)
alert ("Fehler:" + req.status);
else
data = deXmlify (req.responseXML);
console.dir (data);
return true;
default:
return false;
}
}
function updData ()
{
// erstellen des requests
try
{
req = new XMLHttpRequest();
}
catch (e)
{
try
{
req = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
req = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (failed)
{
req = null;
}
}
}
if (req == null)
alert("Error creating request object!");
// anfrage erstellen (GET, url ist localhost,
// request ist asynchron
var url = 'http://localhost/~georg/bilder/ajax+json/ajax.php';
req.open("GET", url, true);
// Beim abschliessen des request wird diese Funktion ausgeführt
req.onreadystatechange = getData;
req.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
req.send(null);
}
//]]>
</script>
</head>
<body>
<script language="javascript">
//<![CDATA[
updData ();
console.log ('%s', 'jokus');
//]]>
</script>
</body>
</html>
Loading…
Cancel
Save