이 당신에게 DOM을 사용하여 좋은 시작과 아이디어를 줄 것이다 :
uses
MSHTML,
ActiveX,
ComObj;
procedure DocumentFromString(Document: IHTMLDocument2; const S: WideString);
var
v: OleVariant;
begin
v := VarArrayCreate([0, 0], varVariant);
v[0] := S;
Document.Write(PSafeArray(TVarData(v).VArray));
Document.Close;
end;
function StripMultipleChar(const S: string; const C: Char): string;
begin
Result := S;
while Pos(C + C, Result) <> 0 do
Result := StringReplace(Result, C + C, C, [rfReplaceAll]);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Document: IHTMLDocument2;
Elements: IHTMLElementCollection;
Element: IHTMLElement;
I: Integer;
Line: string;
begin
Document := CreateComObject(CLASS_HTMLDocument) as IHTMLDocument2;
DocumentFromString(Document, '<head>...'); // your HTML here
Elements := Document.all.tags('A') as IHTMLElementCollection;
for I := 0 to Elements.length - 1 do
begin
Element := Elements.item(I, '') as IHTMLElement;
Memo1.Lines.Add('A HREF=' + Element.getAttribute('HREF', 2));
Memo1.Lines.Add('A innerText=' + Element.innerText);
// Text is returned immediately before the element
Line := (Element as IHTMLElement2).getAdjacentText('beforeBegin');
// Line => "Mittwoch, 30. März 2011 12:01 <dir>" OR:
// Line => "Mittwoch, 9. Februar 2005 17:14 113"...
// I don't know what is the actual delimiter:
// It could be [space] or [tab] so we need to normalize the Line
// If it's tabs then it's easier because the timestamps also contains spaces
Line := Trim(Line);
Line := StripMultipleChar(Line, #32); // strip multiple Spaces sequences
Line := StripMultipleChar(Line, #9); // strip multiple Tabs sequences
// TODO: ParseLine (from right to left)
Memo1.Lines.Add(Line);
Memo1.Lines.Add('-------------');
end;
end;
출력 :
A HREF=/SubDir/
A innerText=SubDir
Mittwoch, 30. März 2011 12:01 <dir>
-------------
A HREF=/file.txt
A innerText=file.txt
Mittwoch, 9. Februar 2005 17:14 113
-------------
편집 :을 변경 한
구현은 으로 단순화되었습니다.. 나는 전 버전이 더 에 최적화되어 있다고 믿었습니다. 라인의 길이가 매우 짧다는 사실을 고려하면 성능에는 별다른 차이가 없습니다.
샘플이 있습니까? 실제로는 표준 "디렉토리 목록"이 없습니다 (실제로 서버 및 운영 체제에 따라 * 여러 *가 있음). 그리고 작업 중인지 알지 못하고 무엇을 할 수 있는지 말할 필요가 없습니다. 할 필요가. –
필요한 것은 트리를 만드는 HTML 파서이므로 모든 "A"태그를 쉽게 찾고 필요에 따라 작동 할 수 있습니다. – ComputerSaysNo
서버가 WebDAV를 지원하면보다 견고한 솔루션이 WebDAV 클라이언트 라이브러리로 구현하기 쉽습니다. 모든 서버 버전 업데이트 또는 소프트웨어 변경으로 인해이 HTML 출력과 관련된 파서가 손상됩니다. – mjn