,,,,
Disney
,Pippo
,,Pluto
,,Paperino
,,,Paperina
,,,,Minnie
,,,Paperone
,,,Paperoga
,,Gambadilegno
,Topolino
,,Basettoni
,,Clarabella
,,Paperinik
,,,Orazio
<div class="tf-tree">
<ul>
<li>
<span class="tf-nc">Disney</span>
<ul>
<li><span class="tf-nc">Pippo</span>
<ul>
<li><span class="tf-nc">Pluto</span></li>
<li><span class="tf-nc">Paperino</span>
<ul>
<li><span class="tf-nc">Paperina</span>
<ul>
<li><span class="tf-nc">Minnie</span></li>
</ul>
<li><span class="tf-nc">Paperone</span>
<li><span class="tf-nc">Paperoga</span>
</li>
</ul>
</li>
</li>
<li><span class="tf-nc">Gambadilegno</span></li>
</ul>
</li>
<li><span class="tf-nc">Topolino</span>
<ul>
<li><span class="tf-nc">Basettoni</span></li>
<li><span class="tf-nc">Clarabella</span>
<li><span class="tf-nc">Paperinik</span>
<ul>
<li><span class="tf-nc">Orazio</span></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
Concettualmente dai tuoi dati sembrerebbe che mancano delle informazioni per fare quello che vuoi tu. Ad esempio ad ogni voce dovrebbe essere corrisposta un id che identifica la voce, e anche un id padre che identifica qual'è l'id del nodo superiore. Altrimenti è impossibile fare quello che chiediVedo con piacere che la mente del programmatore è diversa da quella del sistemista ;D
partirei dal DOM, anzi da THTMLDocument (che eredita da TDOMLDocument), se non ricordo male è nella unit dom_html del package fcl-xml.
Chiaramente il parser della tua struttura dati lo dovrai fare tu, ma credo che questo possa essere un buon approccio.
procedure ....
var sl: TStringList;
begin
sl:=TStringList.Create;
try
sl.LoadFromFile(...);
// loop per scansione righe e generazione HTML
finally
sl.Free;
end;
end;
Function ContaVirgole(T:String):Integer;
Var
MyParse: TStringList;
Conta,I:integer;
begin
MyParse:=TStringList.Create;
MyParse.StrictDelimiter:=True;
MyParse.Delimiter := ',';
MyParse.DelimitedText:=T;
Conta:=0;
For I:=0 to MyParse.Count -1 do
begin
If Trim(MyParse[I])='' then
Begin
Inc(Conta);
end
else
Begin
Result:=Conta;
Break;
end;
end;
end;
Function Splittamelo(T:String):string;
var
MyParse: TStringList;
begin
MyParse:=TStringList.Create;
MyParse.StrictDelimiter:=True;
MyParse.Delimiter := ':';
MyParse.DelimitedText:=T;
Result:= StringReplace(MyParse[0],',','', [RfReplaceall]);
MyParse.Free;
end;
program project_00261;
uses sysutils, Classes;
Function ContaVirgole(T:String):Integer;
Var
MyParse: TStringList;
Conta,I:integer;
begin
MyParse:=TStringList.Create;
MyParse.StrictDelimiter:=True;
MyParse.Delimiter := ',';
MyParse.DelimitedText:=T;
Conta:=0;
For I:=0 to MyParse.Count -1 do
begin
If Trim(MyParse[I])='' then
Begin
Inc(Conta);
end
else
Begin
Result:=Conta;
Break;
end;
end;
MyParse.Free;
end;
Function Splittamelo(T:String):string;
var
MyParse: TStringList;
begin
MyParse:=TStringList.Create;
MyParse.StrictDelimiter:=True;
MyParse.Delimiter := ':';
MyParse.DelimitedText:=T;
Result:= StringReplace(MyParse[0],',','', [RfReplaceall]);
MyParse.Free;
end;
Procedure ContaSplitta(T:String; out Virgole: integer; out TestoSplittato: string);
Var
MyParse: TStringList;
Conta,I:integer;
begin
MyParse:=TStringList.Create;
MyParse.StrictDelimiter:=True;
MyParse.Delimiter := ',';
MyParse.DelimitedText:=T;
Conta:=0;
For I:=0 to MyParse.Count -1 do
begin
If Trim(MyParse[I])='' then
Begin
Inc(Conta);
end
else
Begin
Virgole:=Conta;
Break;
end;
end;
// calcola testo splittato
TestoSplittato:=copy(T, Conta + 1, Length(T) - Conta);
MyParse.Free;
end;
procedure Test_ContaSplitta(sl: TStringList);
var I: integer;
s: string;
ResVirgole: integer;
ResSplit: string;
begin
Writeln('Test_ContaSplitta');
Writeln;
for I := 0 to sl.Count - 1 do
begin
ContaSplitta(sl[I], ResVirgole, ResSplit);
s := Format('Testo: %-20.20s - Livello: %2d - Split: %s',
[
sl[I],
ResVirgole,
ResSplit
] );
Writeln( s );
end;
end;
procedure Test_ContaVirgole_e_Splittatamelo(sl: TStringList);
var I: integer;
s: string;
begin
Writeln('Test_ContaVirgole_e_Splittatamelo');
Writeln;
for I := 0 to sl.Count - 1 do
begin
s := Format('Testo: %-20.20s - Livello: %2d - Split: %s',
[
sl[I],
ContaVirgole(sl[I]),
Splittamelo(sl[I])
] );
Writeln( s );
end;
end;
procedure Test_Scansione(sl: TStringList);
const INIZIO_LIVELLO = 'inizio livello';
FINE_LIVELLO = 'fine livello';
var I, Livello, ResLivello: integer;
s, ResSplittato: string;
begin
Livello:= -1;
for I:=0 to sl.Count-1 do
begin
s:=sl[I];
ContaSplitta(s, ResLivello, ResSplittato);
// test nuovo livello
if ResLivello > Livello then
begin
Livello:=ResLivello;
WriteLn(StringOfChar(' ', Livello*2) + INIZIO_LIVELLO);
end;
// test fine livello
if ResLivello < Livello then
begin
WriteLn(StringOfChar(' ', Livello*2) + FINE_LIVELLO);
Livello:=ResLivello;
end;
// scrittura elemento
WriteLn( StringOfChar(' ', Livello*2) + ResSplittato);
end;
end;
var Albero: TStringList;
begin
Albero:=TStringList.Create;
try
// valorizza Albero a runtime
Albero.Add('Disney');
Albero.Add(',Pippo');
Albero.Add(',,Pluto');
Albero.Add(',,Paperino');
Albero.Add(',,,Paperina');
Albero.Add(',,,,Minnie');
Albero.Add(',,,Paperone');
Albero.Add(',,,Paperoga');
Albero.Add(',,Gambadilegno');
Albero.Add(',Topolino');
Albero.Add(',,Basettoni');
Albero.Add(',,Clarabella');
Albero.Add(',,Paperinik');
Albero.Add(',,,Orazio');
Test_ContaVirgole_e_Splittatamelo(Albero);
WriteLn;
WriteLn;
WriteLn;
Test_ContaSplitta(Albero);
WriteLn;
WriteLn;
WriteLn;
Test_Scansione(Albero);
finally
Albero.Free;
end;
end.
procedure TForm1.Button1Click(Sender: TObject);
var
F : textfile;
ab : string;
i, l : integer;
begin
memo1.Clear;
AssignFile(F, '/home/a.../Scrivania/db.txt');
Reset(F);
l:=1;
while not EOF(F) do begin
i:=0;
ReadLn(F, ab);
While (Pos(',', ab) > 0) Do
begin
Delete(ab, Pos(',', ab), 1);
i:=i+1;
end;
if (i=4) and (l=1) then memo1.Append('<ul style=" background-color: #BFBFBF;">');
if l>1 then
case i of
0 : memo1.Append('<li><a href="....">'+ab+'</a></li>');
1 : memo1.Append('<ul ><li><a href="....">'+ab+ '</a></li>');
2 : memo1.Append('</ul ></li><li><a href="....">'+ab+ '</a></li>');
3 : memo1.Append('<ul ></li><li><a href="....">'+ab+ '</a></li>');
4 : memo1.Append('<ul ></li><li><a href="....">'+ab+ '</a></li>');
//showmessage(inttostr(i));
end;
l:=l+1;
end;
memo1.Append('</ul></ul ></ul >');
CloseFile(F);
memo1.Lines.SaveToFile('/home/a.../Scrivania/db.html');
end;
mi sono permesso di fare qualche esperimento
prova così:
crea una form mettici un campo memo e un pulsante.
il codice deve essere aggiustato con i <ul> </ul> e <li> </li> come ti servono ma come esempio penso che ti possa servire.
Vale la stessa risposta che ho dato a nomorelogic...
Grazie anche a te...
Al capo dei Fremen invece chiederei...