note 114907 deleted from function.xml-parse-into-struct by cmb
| From: | cmb@php.net | Date: | Tue, 09 Oct 2018 12:25:40 +0000 |
| Subject: | note 114907 deleted from function.xml-parse-into-struct by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-209412@lists.php.net to get a copy of this message | ||
Note Submitter: gramo dot gnu at gmail dot com
----
I really need to make the "xml_parse_into_array" function, and I figured out this
"recursive function" solution. (I just test that with ISO-8859-1 charset, but I think it
works with ASCII and UTF-8)
/*------------------------------------------------------------------*/
function xml_parse_into_array($xml)
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DESCRIPCION:
Convierte un texto en xml codificado en ISO-8859-1 a un arreglo de php.
--------------------------------------------------------------------*/
{
$retorno = array();
$p = xml_parser_create();
xml_parser_set_option($p, XML_OPTION_TARGET_ENCODING, "ISO-8859-1");
xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($p, $xml, $elementos, $llaves);
xml_parser_free($p);
$Qprocesados = 0;
$retorno = xml_sub_parse_into_array($elementos, $Qprocesados);
return array($retorno, $Qprocesados);
}
/*------------------------------------------------------------------*/
function xml_sub_parse_into_array($xml, &$i, $abierto = null)
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DESCRIPCION:
--------------------------------------------------------------------*/
{
$retorno = array();
//----------------------------------------
// Caso trivial: TAG COMPLETO
//----------------------------------------
if ($xml[$i]['type'] == 'complete' &&
isset($xml[$i]['value'])) {
$retorno[$xml[$i]['tag']] = trim($xml[$i]['value']);
$i ++;
return $retorno;
//----------------------------------------
// Caso trivial: TAG COMPLETO
//----------------------------------------
} else if ($xml[$i]['type'] == 'cdata' &&
isset($xml[$i]['value'])) {
$retorno = trim($xml[$i]['value']);
$i ++;
return $retorno;
//----------------------------------------
// Caso trivial: TAG VACIO
//----------------------------------------
} else if ($xml[$i]['type'] == 'complete') {
// $retorno = "<".$xml[$i]['tag']." />";
$retorno[$xml[$i]['tag']] = null;
$i ++;
return $retorno;
//----------------------------------------
// Caso recursivo: TAG CON TAGS ADENTRO
//----------------------------------------
} else if ($xml[$i]['type'] == 'open') {
$abierto = $xml[$i]['tag'];
$nivel = $xml[$i]['level'];
if (!isset($retorno[$abierto]))
$retorno[$abierto] = array();
if (isset($xml[$i]['value'])) {
$retorno[$abierto][] = trim($xml[$i]['value']);
}
$i ++;
$termino = false;
while (!$termino && $i < count($xml)) {
if ($xml[$i]['tag'] == $abierto &&
$xml[$i]['type'] == 'close' &&
$xml[$i]['level'] == $nivel ) {
$termino = true;
} else {
$retorno[$abierto][] = xml_sub_parse_into_array($xml, $i, $abierto);
if ($i < count($xml))
if ($xml[$i]['type'] == 'close') {
$i ++;
return $retorno;
}
}
}
return $retorno;
}
}