atendiendo a la justa petición de cluster.......
código modificado para php4 y sin dependencia de base de datos
Código PHP:
<?php
/********************* Xml_Config ******************/
/*
this class uses the php xml parser to construct a multidimensional array with the values of an XML file
*/
class XmlConfig {
$xml_parser = NULL;
$xml_values = array();
$xml_indexs = array();
$xml_struct = array();
$xml_data = "";
$xml_file = "";
function XmlConfig($file = "") {
$this->xml_parser = xml_parser_create();
if ( $file != "" ) {
$this->setXmlFile($file);
return $this->readXml();
} else {
return true;
}
}
function setXmlFile($file) {
$this->xml_file = $file;
return true;
}
function readXml() {
if ( is_file($this->xml_file) && is_readable($this->xml_file) ) {
$data = file_get_contents($this->xml_file);
xml_parse_into_struct($this->xml_parser,$data,$this->xml_values,$this->xml_indexs);
$this->xml_values = $this->xmlRemoveCdata($this->xml_values);
$xml_level_tags = $this->getXmlLevelTags(1);
$this->createXmlStruct($xml_level_tags,$this->xml_struct);
return true;
} else {
return false;
}
}
//i couldnt find a good reason to preserve the cdata, so, i delete it :)
function xmlRemoveCdata($struct) {
$new_struct = array();
foreach ( $struct as $index => $props ) {
if ( $props['type'] != "cdata" ) {
$new_struct[] = $props;
}
}
return $new_struct;
}
//create the multidimensional array depending on the values returned by the php xml parser (recursive function)
function createXmlStruct($xml_level_tags,&$xml_struct) {
foreach ( $xml_level_tags as $index => $keys ) {
$global_ix = $keys['global_index'];
$props = $keys['values'];
$tag_type = $props['type'];
$tag = $props['tag'];
$tag_value = isset($props['value']) ? $props['value'] : "";
$tag_atts = isset($props['attributes']) ? $props['attributes'] : array();
$tag_level = $props['level'];
switch ( $tag_type ) {
case 'cdata':
continue;
break;
case 'open':
if ( !array_key_exists($tag,$xml_struct) ) {
$xml_struct[$tag] = array();
}
$next = count($xml_struct[$tag]);
$xml_struct[$tag][$next] = array();
$xml_struct[$tag][$next]['sub-tags'] = array();
$xml_struct[$tag][$next]['attributes'] = array();
$xml_struct[$tag][$next]['value'] = $tag_value;
$xml_struct[$tag][$next]['type'] = 'open';
$xml_struct[$tag][$next]['level'] = $tag_level;
foreach ( $tag_atts as $attribute => $att_value ) {
if ( !array_key_exists($attribute,$xml_struct[$tag][$next]['attributes']) ) {
$xml_struct[$tag][$next]['attributes'][$attribute] = array();
}
$xml_struct[$tag][$next]['attributes'][$attribute][] = $att_value;
}
$tag_children = array();
$tag_children = $this->getTagChildren($global_ix);
$this->createXmlStruct($tag_children,$xml_struct[$tag][$next]['sub-tags']);
break;
case 'close':
continue;
break;
case 'complete':
if ( !array_key_exists($tag,$xml_struct) ) {
$xml_struct[$tag] = array();
}
$next = count($xml_struct[$tag]);
$xml_struct[$tag][$next] = array();
$xml_struct[$tag][$next]['sub-tags'] = array();
$xml_struct[$tag][$next]['attributes'] = array();
$xml_struct[$tag][$next]['value'] = $tag_value;
$xml_struct[$tag][$next]['type'] = 'complete';
$xml_struct[$tag][$next]['level'] = $tag_level;
foreach ( $tag_atts as $attribute => $att_value ) {
if ( !array_key_exists($attribute,$xml_struct[$tag][$next]['attributes']) ) {
$xml_struct[$tag][$next]['attributes'][$attribute] = array();
}
$xml_struct[$tag][$next]['attributes'][$attribute][] = $att_value;
}
break;
default:
break;
}
}
}
//creates xml data from an xml struct
function createXml(&$xmldata = NULL, $tags = NULL, &$pending_tags = array(), $default_level = 1) {
if ( $xmldata === NULL ) {
$xmldata = &$this->xml_data;
}
$tags = $tags === NULL ? $this->xml_struct : $tags;
foreach ( $tags as $tag_name => $clon_tags ) {
foreach ( $clon_tags as $clon_index => $clon_props ) {
$sub_tags = $clon_props['sub-tags'];
$attributes = $clon_props['attributes'];
$type = $clon_props['type'];
$level = $clon_props['level'];
$value = $clon_props['value'];
$atts = "";
$tab = str_repeat("\t",$level);
$top = count($pending_tags);
for ( $i = $top; $i >=$level; $i-- ) {
$levelname = "level_".$i;
if ( array_key_exists($levelname,$pending_tags) ) {
$xmldata .= $pending_tags[$levelname];
unset($pending_tags[$levelname]);
}
}
$levelname = "level_".$level;
foreach ( $attributes as $att_name => $att_clons ) {
foreach ( $att_clons as $index_clon => $att_value ) {
$atts .= $att_name.'="'.$att_value.'" ';
}
}
$atts = empty($attributes) ? "" : " ".$atts;
$atts = rtrim($atts);
if ( $type == 'open' ) {
$xmldata .= $tab."<".$tag_name.$atts.">".$value."\n";
$pending_tags[$levelname] = $tab."</".$tag_name.">"."\n";
} else {
if ( $value == "" ) {
$xmldata .= $tab."<".$tag_name.$atts."/>"."\n";
} else {
$xmldata .= $tab."<".$tag_name.$atts.">".$value."</".$tag_name.">"."\n";
}
}
$this->createXml($xmldata,$sub_tags,$pending_tags,$default_level+1);
}
}
$default_level_name = 'level_'.$default_level;
if ( isset($pending_tags[$default_level_name]) ) {
$xmldata .= $pending_tags[$default_level_name];
unset($pending_tags[$default_level_name]);
}
}
//saves the data to the file and database
function saveXml($file = "") {
$file = $file == "" ? $this->xml_file : $file;
$fp = fopen($file,'w');
$bytes = fwrite($fp,$this->xml_data);
$file = $this->xml_file;
fclose($fp);
if ( $bytes !== false) {
return true;
} else {
return false;
}
}
//get the XML tags in certain level of deepness
function getXmlLevelTags($level) {
$level_tags = array();
$cnt = 0;
foreach ( $this->xml_values as $index => $props ) {
if ( $props['level'] == $level ) {
$level_tags[$cnt]['values'] = $props;
$level_tags[$cnt]['global_index'] = $index;
$cnt++;
}
}
return $level_tags;
}
//get the children tags of some other tag
function getTagChildren($index) {
$children = array();
$tag = $this->xml_values[$index];
$level = $tag['level'];
$target_level = $level + 1;
$index++;
$cnt = 0;
while ( $this->xml_values[$index]['level'] > $level ) {
if ( $target_level == $this->xml_values[$index]['level'] ) {
$children[$cnt]['global_index'] = $index;
$children[$cnt]['values'] = $this->xml_values[$index];
$cnt++;
}
$index++;
}
return $children;
}
}
//probando
$xml = new XmlConfig();
$xml->setXmlFile('helloworld.xml');
$xml->readXml();
echo '<pre>';
var_dump($xml->xml_struct);
echo '</pre>';
?>
Te dejo nuevamente el código para que no dependa de php5 ni de la base de datos (no hay respaldo del archivo en base de datos)
Basicamente lee el archivo XML y lo convierte a una estructura de PHP.
Los campos de la estructura PHP son:
- value : contenido de la etiqueta XML
- sub-tags: (array) todas las etiquetas anidadas, cada etiqueta es en si misma una estructura de php igual a esta, es decir, cada elemento de este arreglo de subtags, tiene a su vez otro elemento subtags para contener mas etiquetas anidadas.
- attributes: (array) Propiedades de la etiqueta XML
- type: (string)si la etiqueta es de apertura, o unica (open, complete)
- level: (int) Nivel de anidacion
Estoy seguro que si haces la prueba y estudias un poco el código entenderas que esta información es valiosa para manipular los datos XML desde un script php.
EL método createXml hace la conversión inversa que readXml, de la estructura php pasa a XML listo para er guardado con el método saveXml().
si indicas cual es el objetivo final de querer usar ese html tal vez se te pueda ayudar un poco más,
un saludo