Ver Mensaje Individual
  #7 (permalink)  
Antiguo 01/12/2010, 12:14
Avatar de masterpuppet
masterpuppet
Software Craftsman
 
Fecha de Ingreso: enero-2008
Ubicación: Montevideo, Uruguay
Mensajes: 3.550
Antigüedad: 16 años, 3 meses
Puntos: 845
Respuesta: insertar registro al principio de un xml

Podría ser así:

Código PHP:
Ver original
  1. try{
  2.  
  3.     $dom = new DOMDocument();          
  4.     $dom->load('simple.xml');
  5.  
  6.     $node = $dom->createElement('noticia');        
  7.     $node->setAttribute('id', $Nid);  
  8.     $node->setAttribute('titulo', $Ntitulo);    
  9.     $node->setAttribute('fecha', $Nfecha);    
  10.     $node->setAttribute('contenido', $Ncontenido);
  11.  
  12.     $xpath = new DOMXPath($dom);        
  13.     $first = $xpath->query('//noticia[1]')->item(0);
  14.  
  15.     if (null === $first) {
  16.         $dom->documentElement->appendChild($node);            
  17.     } else {
  18.         $first->parentNode->insertBefore($node, $first);
  19.     }
  20.  
  21.     $max   = 10;
  22.     $total = $xpath->evaluate('count(//noticia)');
  23.            
  24.     if ($total > $max) {
  25.         $nodeList = $xpath->query(sprintf('//noticia[position() > %s]', $max));                    
  26.         foreach($nodeList as $node) {                    
  27.             $dom->documentElement->removeChild($node);
  28.         }
  29.     }            
  30.            
  31.     $dom->save('simple.xml');
  32.  
  33. } catch (Exception $e) {
  34.      //handle exception
  35. }

Nota: podrias extender la clase DOMDocument y agregarle toda la funcionalidad que necesitas.

Salu2.