Ver Mensaje Individual
  #4  
Antiguo 13-08-2007
Avatar de b3nshi
b3nshi b3nshi is offline
Miembro
 
Registrado: feb 2005
Ubicación: Tucuman - Argentina
Posts: 164
Reputación: 22
b3nshi Va por buen camino
jaja... la verdad q me exprese muy mal y no explique muy bien... en realidad no se si estare hablando con propiedad... desde mi ignorancia voy a tratar de explicar la diferencia entre el archivo de antes y el de ahora...

Primero... use un componente Txmldocument para crear, modificar y abrir archivos xml... y bueno... un componente treeview para verlo...

En mi caso... yo creaba un treeview con estos datos:
dia
-->horario
------->Actividad

Siendo dia... los 7 dias de la semana... y horario (mañana, tarde y noche) y estos datos no podian variar en mi treeview, pero el dato q si podia variar era actividad... ya q podia cargar 0 - 1 o 5 actividades en un horario...

Para crear el fichero utilice este codigo... no es sacado de ningun lugar... lo hice yo con la ayuda de caro y roman para moverme en el treeview. Y bueno... lei bastante sobre archivos xml...

Aqui el codigo
xml1 es el componente Txmldocument
agenda es el componente Treeview
Código Delphi [-]
var
i: integer;
ndia, nage, nhor, nact: IXMLNode;
begin
{Guardo la actividad}
  xml1.filename := ''; //porq uso el mismo componente para abrir, se puede crear en tiempo de ejecucion y este problema no apareceria
 xml1.Active := True;
  //Ahora si creo el xml
  xml1.Version := '1.0';
 nage := Xml1.AddChild('Agenda');
 for i := 0 to agenda.Items.Count - 1 do
   begin
       case plan.Items[i].Level of
         0:begin ndia := nage.AddChild('Dia'); ndia.Attributes['Text']:= agenda.Items[i].Text; end;
         1:begin nhor := ndia.AddChild('Horario'); nhor.Attributes['Text'] := agenda.Items[i].Text;end;
         2:begin nact := nhor.AddChild('Actividad'); nact.Attributes['Text'] := agenda.items[i].Text; end;
        end;
   end; //Fin del for
  //ahora si guardo el archivo xml
  xml1.SaveToFile('agenda\' + trim(enom.text) + '.ext');
  xml1.active:= false;
end; //Fin del procedimiento q crea el fichero
Y asi quedo guardado el archivo XML...
Código:
<?xml version="1.0"?>


<Agenda>
<Dia Text="Lunes"><Horario Text="Mañana"><Actividad Text="La actividad"/></Horario><Horario Text="Tarde"/></Horario><Horario Text="Noche"/></Dia>
<Dia Text="Martes"><Horario Text="Mañana"></Horario><Horario Text="Tarde"/><Actividad Text="La actividad"/></Horario><Horario Text="Noche"/></Dia>
<Dia Text="Miercoles"><Horario Text="Mañana"><Actividad Text="La actividad"/></Horario><Horario Text="Tarde"/></Horario><Horario Text="Noche"/><Actividad Text="La actividad"/></Dia>
<Dia Text="Jueves"><Horario Text="Mañana"><Actividad Text="La actividad"/></Horario><Horario Text="Tarde"/></Horario><Horario Text="Noche"/></Dia>
<Dia Text="Viernes"><Horario Text="Mañana"><Actividad Text="La actividad"/></Horario><Horario Text="Tarde"/></Horario><Horario Text="Noche"/></Dia>
<Dia Text="Sabado"><Horario Text="Mañana"></Horario><Horario Text="Tarde"/><Actividad Text="La actividad"/></Horario><Horario Text="Noche"/></Dia></Dia>
<Dia Text="Domingo"><Horario Text="Mañana"><Actividad Text="La actividad"/></Horario><Horario Text="Tarde"/></Horario><Horario Text="Noche"/></Dia></Agenda>
En cuanto al codigo para abrir el archivo y mostrarlo en el treeview es este:
Código Delphi [-]
xml1.loadfromfile('agenda\' + enom.Text +'.ext');
xml1.Active := true;ndia := xml1.DocumentElement.ChildNodes.FindNode('Dia');
repeat  //Agrego al tree view  
 ntdia := agenda.Items.Addchild(nil,ndia.Attributes['Text']);
 nhor := ndia.childnodes.FindNode('Horario');      
   repeat      
      nthor := agenda.Items.AddChild(ntdia,nhor.Attributes['Text']);      
      nact := nhor.ChildNodes.FindNode('Actividad');      
        if nact <> nil then      
            repeat       
              ntact := agenda.Items.AddChild(nthor,nact.Attributes['Text']);
              //Agrego al tree view  
              nact := nact.NextSibling;       
            until nact = nil ; 
      nhor := nhor.NextSibling;
   until nhor = nil; 
ndia := ndia.NextSibling;
until ndia = nil  ;
xml1.Active := false;
En el caso de crear el componente Txmldocument en tiempo de ejecucion al final se debe dar el valor de nil...
Código Delphi [-]
xml1 := nil;

Y bueno... creo q eso seria todo... cualquier duda sigo leyendo y aprendiendo por lo q quizas pueda ayudar mas...

{Saludos}

Última edición por b3nshi fecha: 13-08-2007 a las 20:28:19.
Responder Con Cita