Antes de empezar este post dejenme les comento, como nota muy importante, que yo no hice este script, si no que lo encontre en la web y se me hizo interesante postearlo aqui lo hizo MA Razzaque Rupom , ya que funciona bastante bien y si algun dia tienen que hacer un parseador y no disponen de tiempo, y desean algo que funcione bien, pues para eso esta este script.
Resulta pues que yo estaba revisando algo que hice pero como que no me habia gustado mucho, y asi que me dedique a buscar alternativas que ya estuvieran hechas, para no reinventar la rueda.
Lo que buscaba era un parser de XML a PHP, pero que jalaran muy bien, entonces di con el siguiente codigo.
class.xmltoarray.php
-
<?
-
/**
-
* XMLToArray Generator Class
-
* @author : MA Razzaque Rupom <rupom_315@yahoo.com>, <rupom .bd@gmail.com>
-
* Moderator, phpResource (LINK1http://groups.yahoo.com/group/phpresource/LINK1)
-
* URL: LINK2http://www.rupom.infoLINK2
-
* @version : 1.0
-
* @date 06/05/2006
-
* Purpose : Creating Hierarchical Array from XML Data
-
* Released : Under GPL
-
*/
-
-
class XmlToArray
-
{
-
-
var $xml='';
-
-
/**
-
* Default Constructor
-
* @param $xml = xml data
-
* @return none
-
*/
-
-
function XmlToArray($xml)
-
{
-
$this->xml = $xml;
-
}
-
-
/**
-
* _struct_to_array($values, &$i)
-
*
-
* This is adds the contents of the return xml into the array for easier processing.
-
* Recursive, Static
-
*
-
* @access private
-
* @param array $values this is the xml data in an array
-
* @param int $i this is the current location in the array
-
* @return Array
-
*/
-
-
function _struct_to_array($values, &$i)
-
{
-
-
while ($i++ <count($values)) {
-
switch ($values[$i]['type']) {
-
case 'cdata':
-
break;
-
-
case 'complete':
-
$name = $values[$i]['tag'];
-
$child[$name]= ($values[$i]['value'])?($values[$i]['value']):'';
-
$child[$name] = $values[$i]['attributes'];
-
}
-
}
-
break;
-
-
case 'open':
-
$name = $values[$i]['tag'];
-
$child[$name][$size] = $this->_struct_to_array($values, $i);
-
break;
-
-
case 'close':
-
return $child;
-
break;
-
}
-
}
-
return $child;
-
}//_struct_to_array
-
-
/**
-
* createArray($data)
-
*
-
* This is adds the contents of the return xml into the array for easier processing.
-
*
-
* @access public
-
* @param string $data this is the string of the xml data
-
* @return Array
-
*/
-
function createArray()
-
{
-
$xml = $this->xml;
-
$i = 0;
-
$name = $values[$i]['tag'];
-
$array[$name] = $this->_struct_to_array($values, $i);
-
return $array;
-
}//createArray
-
-
-
}//XmlToArray
-
?>
Para usarlo lo haremos de la siguiente forma,
-
<?php
-
/**
-
* XMLToArray Generator Class
-
* @author : MA Razzaque Rupom <rupom_315@yahoo.com>, </rupom><rupom .bd@gmail.com>
-
* Moderator, phpResource (http://groups.yahoo.com/group/phpresource/)
-
* URL: http://www.rupom.info
-
* @version : 1.0
-
* @date 06/05/2006
-
* Purpose : Creating Hierarchical Array from XML Data
-
* Released : Under GPL
-
*/
-
-
require_once "class.xmltoarray.php";
-
-
//XML Data
-
$xml_data = "
-
<result>
-
<studentname>
-
MA Razzaque
-
</studentname>
-
<institute>
-
RUET
-
</institute>
-
<dept>
-
CSE
-
</dept>
-
<roll>
-
99315
-
</roll>
-
<class>
-
First
-
</class>
-
</result>";
-
-
//Creating Instance of the Class
-
$xmlObj = new XmlToArray($xml_data);
-
//Creating Array
-
$arrayData = $xmlObj->createArray();
-
-
//Displaying the Array
-
echo "<pre>";
-
echo "</pre>";
-
?>
Creo que no hay mucho mas que agregar si desean usenlo, recuerden que usa licencia GPL.
[actualizado] gracias Seraphin [/actualizado]
Vía | XML To Array


Julio 24th, 2008 at 8:51 pm
buenas ajaxman !
en php5 existe la clase SimpleXMLElement:
http://es2.php.net/simplexml
eso es mano de santo hoyga !
Julio 24th, 2008 at 10:50 pm
@gafeman:
Gracias, ahora que lo mencionas ya antes usaba eso, el simpleXMLElement, en mi ex-trabajo, no se porque no me acorde inclusive hay un post en este blog sobre eso.
Talvez no lo recorde, pero bueno es otra opcion, la cual por cierto esta muy bien formada.
saludos y gracias por la visita
Julio 25th, 2008 at 6:36 am
Saludos chabs... oye solo un detalle, en el codigo de tu clase xmltoarray como que hay algunos caracteres que no lucen bien... igual y es cosa de mi navegador
(no, no creo)
Si efectivamente es es solo en el primer codigo.