You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
253 lines
4.3 KiB
253 lines
4.3 KiB
<?php
|
|
|
|
/*
|
|
* xml stream to native vars
|
|
*/
|
|
function xmlToVar ($xr)
|
|
{
|
|
if ($xr->isEmptyElement == TRUE)
|
|
return NULL;
|
|
|
|
$ret = NULL;
|
|
|
|
while ($xr->read () &&
|
|
! ($xr->nodeType == 15 &&
|
|
$xr->name == 'variable'))
|
|
{
|
|
print "[DEBUG:xmlToVar] " . $xr->nodeType . " / " . $xr->name .
|
|
" / " . $xr->value . "\n";
|
|
if ($xr->nodeType == 3 && $xr->hasValue == TRUE)
|
|
$ret = $xr->value;
|
|
}
|
|
|
|
return $ret;
|
|
}
|
|
|
|
function xmlToArray ($xr)
|
|
{
|
|
$ret = array ();
|
|
|
|
if ($xr->isEmptyElement == TRUE)
|
|
return $ret;
|
|
|
|
while ($xr->read () &&
|
|
! ($xr->nodeType == 15 &&
|
|
$xr->name == 'array'))
|
|
{
|
|
print "[DEBUG:xmlToArray/1] " . $xr->nodeType . " / " . $xr->name .
|
|
" / " . $xr->value . "\n";
|
|
if ($xr->nodeType == 1 &&
|
|
$xr->name == 'item')
|
|
{
|
|
$key = $xr->getAttribute ('key');
|
|
|
|
while ($xr->read () &&
|
|
! ($xr->nodeType == 15 &&
|
|
$xr->name == 'item'))
|
|
{
|
|
print "[DEBUG:xmlToArray/2] " . $xr->nodeType . " / " . $xr->name .
|
|
" / " . $xr->value . "\n";
|
|
if ($xr->nodeType == 1)
|
|
{
|
|
switch ($xr->name)
|
|
{
|
|
case 'variable':
|
|
$ret[$key] = xmlToVar ($xr);
|
|
$type = $xr->getAttribute ('type');
|
|
settype ($ret[$key], $type);
|
|
break;
|
|
|
|
case 'array':
|
|
$ret[$key] = xmlToArray ($xr);
|
|
break;
|
|
|
|
case 'class':
|
|
$class = $xr->getAttribute ('class');
|
|
$ret[$key] = new $class ($xr);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return $ret;
|
|
}
|
|
|
|
/*
|
|
* native vars to xml stream
|
|
*/
|
|
function varToXml ($xw, $name, $val)
|
|
{
|
|
$xw->startElement ('variable');
|
|
|
|
if ($name !== NULL)
|
|
$xw->writeAttribute ('name', $name);
|
|
|
|
$xw->writeAttribute ('type', gettype ($val));
|
|
|
|
$xw->text ($val);
|
|
|
|
$xw->endElement ();
|
|
}
|
|
|
|
function arrayToXml ($xw, $name, $arr)
|
|
{
|
|
$xw->startElement ('array');
|
|
|
|
if ($name !== NULL)
|
|
$xw->writeAttribute ('name', $name);
|
|
|
|
foreach ($arr as $key => $val)
|
|
{
|
|
$xw->startElement ('item');
|
|
$xw->writeAttribute ('key', $key);
|
|
|
|
if (is_array ($val))
|
|
{
|
|
arrayToXml ($xw, NULL, $val);
|
|
}
|
|
else if (is_object ($val))
|
|
{
|
|
if (in_array ('toXml', get_class_methods ($val)) == TRUE)
|
|
{
|
|
$val->toXml ($xw);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
varToXml ($xw, NULL, $val);
|
|
}
|
|
|
|
$xw->endElement ();
|
|
}
|
|
|
|
$xw->endElement ();
|
|
}
|
|
|
|
function xmlify ($val)
|
|
{
|
|
$xw = new xmlWriter ();
|
|
$xw->openMemory ();
|
|
$xw->setIndent (TRUE);
|
|
$xw->startDocument ('1.0', 'UTF-8');
|
|
|
|
if (is_array ($val))
|
|
{
|
|
arrayToXml ($xw, NULL, $val);
|
|
}
|
|
else if (is_object ($val))
|
|
{
|
|
if (in_array ('toXml', get_class_methods ($val)) == TRUE)
|
|
{
|
|
$val->toXml ($xw);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
varToXml ($xw, NULL, $val);
|
|
}
|
|
|
|
$res = $xw->outputMemory(TRUE);
|
|
unset ($xw);
|
|
|
|
return $res;
|
|
}
|
|
|
|
function deXmlify ($xmldata)
|
|
{
|
|
$ret = NULL;
|
|
|
|
// print "<pre>";
|
|
// var_dump (str_replace (
|
|
// array ("<", ">"), array ("<", ">") , $xmldata));
|
|
// print "</pre>";
|
|
// exit (1);
|
|
|
|
$xr = new XMLReader ();
|
|
$xr->XML ($xmldata);
|
|
|
|
$xr->read ();
|
|
|
|
switch ($xr->name)
|
|
{
|
|
case 'variable': $ret = xmlToVar ($xr); break;
|
|
case 'array': $ret = xmlToArray ($xr); break;
|
|
case 'class':
|
|
$class = $xr->getAttribute ('class');
|
|
$ret = new $class ($xr);
|
|
}
|
|
|
|
$xr->close ();
|
|
|
|
return $ret;
|
|
}
|
|
|
|
class c_xmlify
|
|
{
|
|
function __construct ($xr)
|
|
{
|
|
if ($xr->isEmptyElement == TRUE)
|
|
return;
|
|
|
|
while ($xr->read () &&
|
|
! ($xr->nodeType == 15 &&
|
|
$xr->name == 'class'))
|
|
{
|
|
if ($xr->nodeType == 1)
|
|
{
|
|
$name = $xr->getAttribute ('name');
|
|
|
|
switch ($xr->name)
|
|
{
|
|
case 'variable':
|
|
$this->$name = xmlToVar ($xr);
|
|
$type = $xr->getAttribute ('type');
|
|
settype ($this->$name, $type);
|
|
break;
|
|
|
|
case 'array':
|
|
$this->$name = xmlToArray ($xr);
|
|
break;
|
|
|
|
case 'class':
|
|
$class = $xr->getAttribute ('class');
|
|
$this->$name = new $class ($xr);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
function toXml ($xw, $name = NULL)
|
|
{
|
|
$xw->startElement ('class');
|
|
|
|
if ($name !== NULL)
|
|
$xw->writeAttribute ('name', $name);
|
|
|
|
$xw->writeAttribute ('class', get_class ($this));
|
|
|
|
foreach (get_object_vars ($this) as $var => $val)
|
|
{
|
|
if (is_array ($val))
|
|
{
|
|
arrayToXml ($xw, $var, $val);
|
|
continue;
|
|
}
|
|
|
|
if (is_object ($val))
|
|
{
|
|
if (in_array ('toXml', get_class_methods ($val)) == TRUE)
|
|
{
|
|
$val->toXml ($xw, $var);
|
|
}
|
|
continue;
|
|
}
|
|
|
|
varToXml ($xw, $var, $val);
|
|
}
|
|
|
|
$xw->endElement();
|
|
}
|
|
};
|
|
|
|
?>
|