Browse Source
erste Version von xmlify fuer JavaScript hinzugefügt. Im Moment nur deXmlify...warscheinlich brache ich auch ein xmlify() nicht.
master
erste Version von xmlify fuer JavaScript hinzugefügt. Im Moment nur deXmlify...warscheinlich brache ich auch ein xmlify() nicht.
master
4 changed files with 300 additions and 4 deletions
-
32ajax+json/ajax.php
-
11js/helper.js
-
161js/xmlify.js
-
100testxmlify.html
@ -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; |
||||
|
} |
||||
|
|
||||
@ -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; |
||||
|
} |
||||
@ -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> |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue