Mega Code Archive

 
Categories / Delphi / Examples
 

HTML DOM parser + NODE viewer

Title: HTML DOM parser + NODE viewer Question: Resolve DOM tree using Parse function. Also build basic tree with preview option. Answer: Download complete article: http://web.vip.hr/inga.vip/domnodes.zip if you don't have mshtml_tlb already installed,just import ActiveX named "Microsoft HTML Object Library" unit DOMparser; interface uses windows,MSHTML_tlb, ActiveX, ComObj; const IID_IPersistStreamInit : TGUID = '{7FD52380-4E07-101B-AE2D-08002B2EC713}'; type TDomStringParser=class private pDoc : IHTMLDocument2; pNewDoc : IHTMLDocument2; pPersist : IPersistStreamInit; pMS : IMarkupServices; pMC : IMarkupContainer; pMkStart : IMarkupPointer; pMkFinish : IMarkupPointer; pBody : IHTMLElement; didInit : boolean; IsInitialized:longbool; protected function LoadString (var OutData:string;cFileName:string):longbool; function CreateAll:longbool; function FreeAll:longbool; public property Document:IHTMLDocument2 read pNewDoc; constructor Create; destructor Destroy;override; function ParseDocument (const FileName:string):longbool; function ParseString (const HtmlString:string):longbool; end; implementation { TDomStringParser } function TDomStringParser.LoadString (var OutData:string;cFileName:string):longbool; var fLen:cardinal; fH:cardinal; xS:cardinal; xH:cardinal; begin result:=false; fH:=CreateFile(pointer(cFileName), GENERIC_READ ,0,0,OPEN_EXISTING,0,0); if fH=INVALID_HANDLE_VALUE then exit; xS:=GetFileSize(fh,addr(xH)); setlength(OutData,xS); if ReadFile(fH,pointer(OutData)^,length(OutData),fLen,0) then result:=true; CloseHandle(fH); end; function TDomStringParser.ParseDocument (const FileName:string):longbool; var cString:string; cW:widestring; begin result:=false; if not IsInitialized then exit; if not LoadString(cString,FileName) then exit; cW:=widestring(cString); setlength(cString,0); //Po?ni... pMS.ParseString(word(cW[1]), 0, pMC, pMkStart, pMkFinish); if (pMC nil) then begin pMC.QueryInterface(IID_IHTMLDocument, pNewDoc); if (pNewDoc nil) then result:=true; end; end; function TDomStringParser.ParseString( const HtmlString: string): longbool; var cW:widestring; begin result:=false; if not IsInitialized then exit; cW:=widestring(HtmlString); //Po?ni... pMS.ParseString(word(cW[1]), 0, pMC, pMkStart, pMkFinish); if (pMC nil) then begin pMC.QueryInterface(IID_IHTMLDocument, pNewDoc); if (pNewDoc nil) then result:=true; end; end; function TDomStringParser.CreateAll: longbool; begin result:=false; CoCreateInstance(CLASS_HTMLDocument, nil, CLSCTX_INPROC_SERVER, IID_IHTMLDocument2, pDoc); if pDoc = nil then exit; pDoc.QueryInterface(IID_IPersistStreamInit, pPersist); if pPersist = nil then exit; pPersist.InitNew; pPersist._Release; pDoc.QueryInterface(IID_IMarkupServices, pMS); if pMS = nil then exit; pMS.CreateMarkupPointer(pMkStart); pMS.CreateMarkupPointer(pMkFinish); result:=true; end; function TDomStringParser.FreeAll: longbool; begin IsInitialized:=false; end; constructor TDomStringParser.Create; begin inherited; IsInitialized:=CreateAll; end; destructor TDomStringParser.Destroy; begin FreeAll; end; end.