본문 바로가기

프로그래밍/Delphi

IE TWebBrowser없이 DOM 사용하기

TWebBrower에 웹페이지를 네비게이션 한 후 WB.Document as IHTMLDocument2 를 이용해 문서를 얻고 DOM 탐색을 했던것 처럼 TWebBrowser에 의존하지 않고 독립된 DOM을 다음과 같이 사용할 수 있습니다.

 

...
const
  HTML =
  '
' + '' + 'Value' + '
' + '
'; ... procedure TForm3.Button1Click(Sender: TObject); var doc: OleVariant; el: OleVariant; i: Integer; begin doc := coHTMLDocument.Create as IHTMLDocument2; doc.write(HTML); doc.close; ShowMessage(doc.body.innerHTML); for i := 0 to doc.body.all.length - 1 do begin el := doc.body.all.item(i); if (el.tagName = 'LABEL') and (el.className = 'tvLabel') then Memo2.Lines.Add('TagName:LABEL=' + el.innerText); if (el.tagName = 'SPAN') and (el.className = 'tvValue') then Memo2.Lines.Add('TagName:SPAN=' + el.innerText); end; end;