Ver Mensaje Individual
  #2 (permalink)  
Antiguo 24/02/2010, 19:40
Avatar de jackson666
jackson666
 
Fecha de Ingreso: noviembre-2009
Ubicación: Buenos Aires, Argentina
Mensajes: 1.971
Antigüedad: 16 años, 10 meses
Puntos: 65
Respuesta: Generar XML e ir añadiendo nodos

1)
Código PHP:
# Te falto un '=' !!!
 
if( $doc=false) {
  
$doc = new DOMDocument("1.0");
  } 
2)
Código PHP:
# Es load(), no Load()...
$doc->Load( '../banc_preguntes.xml' ); 
3) No se si sabias, pero createElement() tiene un segundo parametro opcional, en donde ingresas directamente un valor string, ahorrandote de usar createTextNode(), asi:

Código PHP:
Ver original
  1. $pregunta = $doc->createElement("pregunta", $_POST['pregunta'] );

4) Te estan faltando pasos mira:

Código PHP:
# Traes o creas el tag "questions"
$r= $doc->getElementById('questions');
  if( 
$r==null ) 
  {
      
$r = $doc->createElement( "questions" );
      
$r->setIdAttribute("xml:id", true);
      
$doc->appendChild( $r );
  }

   
# creas un tag "questio" (hijo de questions)
   
$b = $doc->createElement( "questio" );
 
   
# creas un tag "pregunta" (hijo de "questio" y de "questions")
   
$pregunta = $doc->createElement( "pregunta" );
   
$pregunta->appendChild( $doc->createTextNode( $_POST['pregunta'] ));

   
# Se lo agregas al tag padre "questio"
   
$b->appendChild( $pregunta );
  
   
# creas un tag "resposta"
   
$answer = $doc->createElement( "resposta" );
   
$answer->appendChild( $doc->createTextNode( $_POST['resposta'] ));
   
   
# Se lo agregas al tag padre "questio"
   
$b->appendChild( $answer );
  
   
# Te falto un $r->appendChild($b);

   # Agregas el tag "questio" pero te falta agregarselo a "questions" !! 
   
$doc->appendChild( $b );

   
$doc->save( '../banc_preguntes.xml'  ); //guardo a XML
?> 
__________________
HV Studio
Diseño y desarrollo web

Última edición por jackson666; 24/02/2010 a las 19:48