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

From: Date: Thu, 24 Apr 2014 04:51:08 +0000
Subject: note 114907 added to function.xml-parse-into-struct
Groups: php.notes 
Request: Send a blank email to php-notes+get-199323@lists.php.net to get a copy of this message
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; } } ---- Server IP: 72.52.91.14 Probable Submitter: 190.146.247.164 ---- Manual Page -- http://php.net/manual/en/function.xml-parse-into-struct.php Edit -- https://master.php.net/note/edit/114907 Del: integrated -- https://master.php.net/note/delete/114907/integrated Del: useless -- https://master.php.net/note/delete/114907/useless Del: bad code -- https://master.php.net/note/delete/114907/bad+code Del: spam -- https://master.php.net/note/delete/114907/spam Del: non-english -- https://master.php.net/note/delete/114907/non-english Del: in docs -- https://master.php.net/note/delete/114907/in+docs Del: other reasons-- https://master.php.net/note/delete/114907 Reject -- https://master.php.net/note/reject/114907 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#199323) next »