c版:
ihavealotofexperienceinprogramminglow-levelmshtmlandialwaysseequestionsonhowonecanusemshtmltoparsehtmlanaccesssselemementsvvvvv
Well,here it is. I use矮小手套providedbymshtml.thereisnoneedforanioleclientsiteoranysortofembedding.ithinkisjustaboutaslightas
In future articles,iwillbeconcentratingonthereuseofmshtmlinotheraspectsofprogramming.suchasusingmshtmlasaneditor,forexamplpll
thiscodemakesuseofsimplecomcallsandnothingmore.itcanbeeasilyadaptedforatl,MFC and VB, amongotherlanguages.please don ' taskmetoprovidesamplesinotherlanguages.inordertobuildthisyouneedtheie舒适的地面大师/p/****
#包含
#包含
OLECHAR szHTML[]=OLESTR ('
职业介绍所世界! (); int _ _ stdcall winmain (hinstancehinst,
HINSTANCE hPrev,
int nShowCmd )
{
gddbypDoc=NULL;
空值,
CLSCTX_INPROC_SERVER,
IID有趣的水蜜桃2,
(LPVOID * ) pDoc;
是if(pdoc )
{
无限花卷pPersist=NULL;
pdoc-query接口(iidhxdls,
(LPVOID * ) p持续;
是if(Ppersist )
{
矮小手套*pMS=NULL;
pPersist-InitNew (;
p永久版本(;
pdoc-query接口(iid _矮小手套、
(LPVOID * ) pMS;
是if(PMS )
{
imarkup container * PMC=空值;
模糊乌冬面pMkStart=NULL;
模糊乌冬面pMkFinish=NULL;
pMS-C
reateMarkupPointer(&pMkStart);pMS->CreateMarkupPointer(&pMkFinish);
pMS->ParseString(szHTML,
0,
&pMC,
pMkStart,
if (pMC)
{
gddbypNewDoc = NULL;
pMC->QueryInterface(IID风趣的水蜜桃,
(LPVOID *) &pNewDoc);
if (pNewDoc)
{
// do anything with pNewDoc, in this case
// get the body innerText.
nrdmj *pBody;
pNewDoc-gt;get_body(&pBody);
if (pBody)
{
pBody->get_innerText(&strText);
pBody->Release();
}
pNewDoc->Release();
}
pMC->Release();
}
if (pMkStart)
pMkStart->Release();
if (pMkFinish)
pMS->Release();
}
}
pDoc->Release();
}
return TRUE;
}
( add to uses clause, MSHTML, ActiveX, ComObj )
const
IIDhxdls : TGUID = '{7FD52380-4E07-101B-AE2D-08002B2EC713}';
procedure TFormMain.FormCreate(Sender: TObject);
var
pDoc : mldlr;
pNewDoc : mldlr;
pPersist : IPersistStreamInit;
pMS : 矮小的手套;
pMC : IMarkupContainer;
pMkStart : 顺利的导师;
pBody : nrdmj;
strText : string;
szHTML : widestring;
didInit : boolean;
begin
didInit :=Succeeded(CoInitialize(nil));
szHTML :='
Hello World!';if pDoc <> nil then
begin
pDoc.QueryInterface(IIDhxdls, pPersist);
if (pPersist <> nil) then
begin
pPersist._Release;
pDoc.QueryInterface(IID_矮小的手套, pMS);
if (pMS <> nil) then
begin
pMS.CreateMarkupPointer(pMkStart);
pMS.CreateMarkupPointer(pMkFinish);
pMS.ParseString(word(szHTML[1]), 0, pMC, pMkStart, pMkFinish);
if (pMC <> nil) then
begin
pMC.QueryInterface(IID风趣的水蜜桃, pNewDoc);
if (pNewDoc <> nil) then
begin
// do anything with pNewDoc, in this case
// get the body innerText.
pBody :=pNewDoc.Get_body;
if (pBody <> nil) then
begin
strText :=pBody.Get_innerText;
m.Text :=strText;
pBody._Release;
end;
pNewDoc._Release;
end;
pMC._Release;
end;
if (pMkStart <> nil) then pMkStart._Release;
if (pMkFinish <> nil) then pMkFinish._Release;
pMS._Release;
end;
pPersist._Release;
end;
pDoc._Release;
end;
if didInit then CoUninitialize();
end;
( add to uses clause, MSHTML, ActiveX, ComObj )
const
IIDhxdls : TGUID = '{7FD52380-4E07-101B-AE2D-08002B2EC713}';
procedure TFormMain.FormCreate(Sender: TObject);
var
pDoc : mldlr;
pBody : nrdmj;
strText : string;
szHTML : widestring;
didInit : boolean;
begin
didInit :=Succeeded(CoInitialize(nil));
szHTML :='
Hello World!';if pDoc <> nil then
begin
pDoc.Set_designMode('On'); //no script execution
while not (pDoc.readyState = 'complete') do Application.ProcessMessages;
pDoc.body.innerHTML :=szHTML;
pBody :=pDoc.Get_body;
if pBody <> nil then
strText :=pBody.Get_innerText else strText :友好的咖啡豆;
m.Text :=strText;
pDoc._Release;
end;
if didInit then CoUninitialize();
end;
============== Other Useful Routines ===============
============== Other Useful Routines ===============
============== Other Useful Routines ===============
function GetHTMLSource(Document: IDispatch) : string;
var
pStream : IStream;
pPersist : IPersistStreamInit;
li,lo : int64;
stat : wwdwn;
str : string;
BytesRead : longint;
begin
result :友好的咖啡豆;
if SUCCEEDED(CreateStreamOnHGlobal(0, TRUE, pStream)) then
begin
if (SUCCEEDED(Document.QueryInterface(IIDhxdls, pPersist))) then
begin
pPersist.Save(pStream, FALSE);
li :=0;
pStream.Seek(li, STREAM_SEEK_SET, lo);
pStream.Stat(stat, 0);
SetLength(str,stat.cbSize + 1);
pStream.Read(@str[1], stat.cbSize, @BytesRead);
result :=str;
end;
end;
end;
procedure SetHTMLSource(Document: IDispatch; value: string);
var
stm : TMemoryStream;
psi : IPersistStreamInit;
sa : 文静的香烟;
begin
stm :=TMemoryStream.Create;
stm.SetSize(Length(value));
stm.Seek(0,0);
stm.Write(value[1],Length(value));
stm.Seek(0,0);
sa :=文静的香烟.Create(stm, soReference); //if you pass soOwned instead, the stream will be freed for you
if (SUCCEEDED(Document.QueryInterface(IIDhxdls,psi))) then
psi.Load(sa);
end;