Ver Mensaje Individual
  #1 (permalink)  
Antiguo 10/10/2012, 12:24
Mike1520
 
Fecha de Ingreso: octubre-2012
Mensajes: 3
Antigüedad: 11 años, 7 meses
Puntos: 0
Insertar datos en archivo XML con PHP

Quiero guardar los datos ingresados en formulario (formulario.php)

Código HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Documento sin título</title>
</head>

<body>
<form method="post" action="cargarxml.php">
<fieldset>
<legend>Llenar los datos</legend>
<label for="txtname">Nombre</label>
<input type="text" name="txtname" id="txtname" />
<br /><br />
<label for="txtApellido">Apellido</label>
<input type="text" name="txtApellido" id="txtApellido" /><br /><br />
<label for="list">Pais</label>
<select name="pais" id="list">
<option value="Peru">Peru</option>
<option value="Brasil">Brasil</option>
<option value="Bolivia">Bolivia</option>
</select><br /><br /><br />
<input type="submit" value="Guardar" />
</fieldset>
</form>
</body>
</html> 
en un archivo XML (usuarios.xml)

Código:
<?xml version="1.0" encoding="UTF-8"?>
<personas>
  <user>
    <nombre>Pepe</nombre>
    <apellido>Perez</apellido>
    <pais>Peru</pais>
  </user>
</personas>
Y con esta otra pagina PHP (cargarxml.php) inserto los datos del formulario a usuarios.xml pero no se acumulan sino que se reescriben

Código PHP:
<?php

$name
=$_POST['txtname'];
$apelli=$_POST['txtApellido'];
$pais=$_POST['pais'];

if(
$name != "" && $apelli != "")
{
    
$xml = new DomDocument('1.0''UTF-8');
    
$root $xml->createElement('personas');
    
$root $xml->appendChild($root);
    
    
$user=$xml->createElement('user');
    
$user =$root->appendChild($user);
    
    
$nom=$xml->createElement('nombre',$name);
    
$nom =$user->appendChild($nom);
    
    
$apellidonode=$xml->createElement('apellido',$apelli);
    
$apellidonode=$user->appendChild($apellidonode);
    
    
$paisnode=$xml->createElement('pais',$pais);
    
$paisnode=$user->appendChild($paisnode);
    
    
$xml->formatOutput true;

    
$strings_xml $xml->saveXML();

    
$xml->save('usuarios.xml');

    echo 
'Se guardo correctamente';
}
else
    echo 
'Datos vacios';

?>
Asi que alguien me puede indicar que debo cambiar en el codigo de cargarxml.php?
y si pueden ayudarme para que cada vez que se inserte un nuevo dato en este se guarde al principio

Código:
<?xml version="1.0" encoding="UTF-8"?>
<personas>
  <user>
    <nombre>Juan</nombre>
    <apellido>Paz</apellido>
    <pais>Brasil</pais>
  </user>
  <user>
    <nombre>Pepe</nombre>
    <apellido>Perez</apellido>
    <pais>Peru</pais>
  </user>
</personas>
Gracias de antemano!!!