note 80325 added to function.xml-parse-into-struct

From: Date: Thu, 10 Jan 2008 10:35:23 +0000
Subject: note 80325 added to function.xml-parse-into-struct
Groups: php.notes 
Request: Send a blank email to php-notes+get-134992@lists.php.net to get a copy of this message
I didn't fount an appropiate xml2array translaction for my purpose, so I wrote this: (convert an xml string to an associative array allowing multiple elements with the same name) /////////////////////////////////// Inicio XML // // // // Convierte un XML en un array asociativo cuyos elementos son arrays // (para permitir varios elementos del mismo nombre) // // Limitación: el elemento "attributos" no puede aparecer en el XML, pues corresponde al // de attribs de un tag (un Tag empieza con "T",no "t") // //////////////////////////////////// function xml_analiza($xml) { global $xml_resul,$xml_n,$xml_cont,$xml_attr; $xml_n = 0; $xml_resul = array(); $xml_cont = array(); $xml_attr = array(); $p = xml_parser_create(); //Si quisiéramos distinguir Mayúsculas-Minúsculas en los nombres de etiquetas: xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0); xml_set_element_handler($p, "xml_inicio", "xml_fin"); xml_set_character_data_handler($p, "xml_char"); // Recorta hasta el primer tag del XML: $i = instr($xml,"<" . "?xml"); if (!$i) return array(); $j = instr($i,$xml,"?" . ">"); if (!$j) return array(); $xml = substr($xml,$j+2); if (!xml_parse($p, $xml)) alarma("URG","xml_analiza","XML error: " . xml_error_string(xml_get_error_code($p)) . " en la línea " . xml_get_current_line_number($p) . " ($xml)"); xml_parser_free($p); if (!sizeof($xml_resul)) return array(); return $xml_resul[0]; } function xml_inicio($p, $nombre, $atributos) { global $xml_resul,$xml_n,$xml_cont,$xml_attr; $xml_n++; $xml_resul[] = array(); $xml_cont[] = ""; $xml_attr[] = $atributos; } function xml_fin($p, $nombre) { global $xml_resul,$xml_n,$xml_cont,$xml_attr; $xml_n--; $nuevo = array_pop($xml_resul); $nombre = $nombre; if ($nombre == "attributos") alarma("URG","xml_analiza","Tag con nombre no permitido (attributos)"); $conte = array_pop($xml_cont); $attrib = array_pop($xml_attr); if ($conte) $xml_resul[$xml_n][$nombre][] = $conte; else { if ($nuevo) { if ($attrib) $nuevo["attributos"][] = $attrib; $xml_resul[$xml_n][$nombre][] = $nuevo; } else $xml_resul[$xml_n][$nombre][] = ""; } } function xml_char($p, $data) { global $xml_cont; $xml_cont[sizeof($xml_cont)-1] .= trim(str_replace("\n","",$data)); } function xml_a($a) { // Devuelve un elemento del array XML, p.e.: xml_a($v,"FichaCircuito","Red",0) $n = func_num_args(); for ($i=1;$i<$n;$i++) { $b = func_get_arg($i); if (isset($a[$b])) $a = $a[$b]; else { if (!isset($a[0][$b][0])) return ""; $a = $a[0][$b]; } } return $a; } ---- Server IP: 130.206.1.5 Probable Submitter: 80.26.44.61 ---- Manual Page -- http://www.php.net/manual/en/function.xml-parse-into-struct.php Edit -- https://master.php.net/note/edit/80325 Del: integrated -- https://master.php.net/note/delete/80325/integrated Del: useless -- https://master.php.net/note/delete/80325/useless Del: bad code -- https://master.php.net/note/delete/80325/bad+code Del: spam -- https://master.php.net/note/delete/80325/spam Del: non-english -- https://master.php.net/note/delete/80325/non-english Del: in docs -- https://master.php.net/note/delete/80325/in+docs Del: other reasons-- https://master.php.net/note/delete/80325 Reject -- https://master.php.net/note/reject/80325 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#134992) next »