Ver Mensaje Individual
  #2 (permalink)  
Antiguo 15/12/2013, 12:10
Avatar de NSD
NSD
Colaborador
 
Fecha de Ingreso: mayo-2012
Ubicación: Somewhere
Mensajes: 1.332
Antigüedad: 12 años
Puntos: 320
Respuesta: ¿Como ordenar un xml de varios atributos con PHP?

php tiene funciones nativas para esto que ahora mismo no recuerdo, pero puedes hacer algo a manuela como esto:
Código PHP:
Ver original
  1. <?php
  2. function getBloquesXML($xml, $bloque)
  3. {  
  4.     $tmp_bloques = array();
  5.     $ini = true;
  6.     while($ini !== false)
  7.     {
  8.         $ini = strpos($xml, '<'.$bloque.'>');
  9.         $fin = strpos($xml, '</'.$bloque.'>'); 
  10.         $tmp = substr($xml, ($ini+strlen('<'.$bloque.'>')), $fin-($ini+strlen('<'.$bloque.'>')));
  11.         if(trim($tmp) != '')
  12.             $tmp_bloques[] = $tmp;
  13.         $xml = substr($xml, $fin-($ini+strlen('<'.$bloque.'>')));
  14.     }
  15.     return $tmp_bloques;
  16. }
  17.  
  18. function ordenarXML($xml, $seccion, $bloque, $propiedad, $forma)
  19. {
  20.     $conXML = array('','');
  21.     $ini = strpos($xml, '<'.$seccion.'>');
  22.     $fin = strpos($xml, '</'.$seccion.'>');
  23.    
  24.     $xmlOrdenado = substr($xml, 0, ($ini+strlen('<'.$seccion.'>')));
  25.     $conXML[0] = getBloquesXML($xml, $seccion);
  26.     $conXML[1] = substr($xml, $fin);
  27.    
  28.     $bloques = getBloquesXML($conXML[0][0], $bloque);
  29.     $tmp_arr_val = array();
  30.     $tmp_arr_ind = array();
  31.     foreach($bloques as $indice => $unBloque)
  32.     {
  33.         $tmp_arr_val[] = getBloquesXML($unBloque, $propiedad)[0];
  34.         $tmp_arr_ind[] = $indice;      
  35.     }
  36.     array_multisort($tmp_arr_val, $tmp_arr_ind);
  37.    
  38.     $ini = 0;
  39.     $fin = 0;
  40.     if($forma == 'DESC')
  41.     {
  42.         for($ind=0,$fin=count($tmp_arr_ind); $ind < $fin; $ind++)
  43.             $xmlOrdenado .= $bloques[$tmp_arr_ind[$ind]];
  44.     } else {   
  45.         for($ind=count($tmp_arr_ind)-1, $fin=0; $ind>=$fin; $ind--)
  46.             $xmlOrdenado .= $bloques[$tmp_arr_ind[$ind]];
  47.     }      
  48.    
  49.     $xmlOrdenado .= $conXML[1];
  50.    
  51.     return $xmlOrdenado;
  52. }
  53. // La llamarias asi:
  54. echo(ordenarXML($xml, 'usuarios', 'id' ,'nombre', 'ASC'));
__________________
Maratón de desafíos PHP Junio - Agosto 2015 en FDW | Reglamento - Desafios