class cls_xmlobject
{
/***
*
This class is able to convert array to xml format and reverse.
It uses base64 code to save special characters.
PHP Version 4.0+
***/
var $id = "anyid" /*here you can put any ID which specifies an array*/;
private function fct_solve_array($array)
{
/* löst verschachtelte XML-Knoten in Arrays auf */
if(is_array($array))
{
for($i = 0; $i < sizeof($array); $i++)
{
$newarray[$array["key"][$i]["@"]["name"]] = $this->fct_solve_array($array["key"][$i]["#"]);
}
return $newarray;
}
else
{
return base64_decode($array);
}
}
private function fct_resolving_array($array)
{
/* speichert verschachtelte Arrays in untergeordnete XML Kindelemente auf */
if(is_array($array))
{
foreach($array as $innerRow => $innerkey)
{
$line = '<key name="'.$innerRow.'">'.$this->fct_resolving_array($innerkey).'</key>';
}
return $line;
}
else
{
return base64_encode($array);
}
}
function fct_xml2array($xmlfile)
{
/* wandelt eine aus einem Array erstellte XML datei wieder in ein Array um */
$xml = $this->fct_xmlize(file_get_contents($xmlfile)); # where $data is the xml in the above section.
$people = $xml["data"]["#"]["row"];
for($i = 0; $i < sizeof($people); $i++)
{
$data = $people[$i];
for($h = 0; $h < sizeof($data["#"]["key"]); $h++)
{
$id = $data["@"]["id"];
$name = $data["#"]["key"][$h]["@"]["name"];
$value = $data["#"]["key"][$h]["#"];
$name = base64_decode($name);
$neuesarray[$id][$name]= $this->fct_solve_array($value); //value = ein array?
}
}
return $neuesarray;
}
function fct_array2xml($array, $file)
{
/* wandelt eine XML datei in ein Array um */
$handle = fopen($file, "w++");
$content = '<data>'."\n";
fwrite($handle, $content);
$i = 0;
foreach($array as $result)
{
$content = '<row id="'.$this->k_nr.'_'.$i.'">'."\n";
fwrite($handle, $content);
foreach($result as $resultData => $key)
{
$resultData = base64_encode($resultData);
$content = '<key name="'.$resultData .'">'.$this->fct_resolving_array($key).'</key>'."\n";
fwrite($handle, $content);
}
$content = '</row>'."\n";
fwrite($handle, $content);
$i++;
}
$content = '</data>'."\n";
fwrite($handle, $content);
$close = fclose($handle);
}
private function fct_xmlize($data, $WHITE=1) {
/* wird benötigt um XML Knoten in PHP 4 -- auszulesen */
$data = trim($data);
$vals = $index = $array = array();
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, $WHITE);
if ( !xml_parse_into_struct($parser, $data, $vals, $index) )
{
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($parser)),
xml_get_current_line_number($parser)));
}
xml_parser_free($parser);
$i = 0;
$tagname = $vals[$i]['tag'];
if ( isset ($vals[$i]['attributes'] ) )
{
$array[$tagname]['@'] = $vals[$i]['attributes'];
} else {
$array[$tagname]['@'] = array();
}
$array[$tagname]["#"] = $this->fct_xml_depth($vals, $i);
return $array;
}
private function fct_xml_depth($vals, &$i) {
/* interne Funktion von XMlize */
$children = array();
if ( isset($vals[$i]['value']) )
{
array_push($children, $vals[$i]['value']);
}
while (++$i < count($vals)) {
switch ($vals[$i]['type']) {
case 'open':
if ( isset ( $vals[$i]['tag'] ) )
{
$tagname = $vals[$i]['tag'];
} else {
$tagname = '';
}
if ( isset ( $children[$tagname] ) )
{
$size = sizeof($children[$tagname]);
} else {
$size = 0;
}
if ( isset ( $vals[$i]['attributes'] ) ) {
$children[$tagname][$size]['@'] = $vals[$i]["attributes"];
}
$children[$tagname][$size]['#'] = $this->fct_xml_depth($vals, $i);
break;
case 'cdata':
array_push($children, $vals[$i]['value']);
break;
case 'complete':
$tagname = $vals[$i]['tag'];
if( isset ($children[$tagname]) )
{
$size = sizeof($children[$tagname]);
} else {
$size = 0;
}
if( isset ( $vals[$i]['value'] ) )
{
$children[$tagname][$size]["#"] = $vals[$i]['value'];
} else {
$children[$tagname][$size]["#"] = '';
}
if ( isset ($vals[$i]['attributes']) ) {
$children[$tagname][$size]['@']
= $vals[$i]['attributes'];
}
break;
case 'close':
return $children;
break;
}
}
return $children;
}
private function fct_traverse_xmlize($array, $arrName = "array", $level = 0) {
/* interne Funktion von XMlize */
foreach($array as $key=>$val)
{
if ( is_array($val) )
{
$this->fct_traverse_xmlize($val, $arrName . "[" . $key . "]", $level + 1);
} else {
$GLOBALS['traverse_array'][] = '$' . $arrName . '[' . $key . '] = "' . $val . "\"\n";
}
}
return 1;
}
}