Click here to Skip to main content
15,867,141 members
Articles / Desktop Programming / MFC
Article

Loading and parsing HTML using MSHTML. 3rd way.

Rate me:
Please Sign up or sign in to vote.
4.74/5 (19 votes)
11 May 2002CPOL4 min read 984.3K   5.8K   128   203
Explains how to load HTML code from memory and parse it using MS technologies

Introduction

First let me explain why I called the article "3rd Way". I've already seen such articles on CodeGuru, explaining how to load and parse HTML file from memory. You may ask, so why I'm writing another guide? Well, below I'll show advantages and disadvantages that I found in those ways.

First one, which is also shown in MSDN , is to load HTML code using IStream interface. You can read the article about it here. If all you want is to put a new code into your document, you should definately use this one. But if you'll try to get tags from your document after you load HTML, you will get nothing. Just because they are still in parsing and you have to create an OnDocumentComplete handler and only than start to look inside your document.

When I realized this I went to look for another way that will give me document immediately after submitting a code. And yes, I found it! You can look at the great article by Asher Kobin at CodeGuru. It uses a new interface called IMarkupServices, introduced with MS Internet Explorer 5.0. I picked this code and made my own from it and started using it.... but suddenly I saw that when I'm saving my document to disk, the BODY tag has no attributes! I worked on this problem a whole day, trying to get it working, but... nothing. When you load your HTML code from memory into document, all attributes of BODY tag are gone. Still have no idea why it is happening and will be glad if someone will tell me.

Thus I came to MSDN again and found another, third way to load and parse HTML. I was so happy, so I decided to write my first article to CodeProject about it, which you are reading now :)

Code

For those, advanced programmers, that don't want to read a whole article, I will give a hint: loading HTML code is made by write() method of IHTMLDocument2 interface.

Now I'll explain how to do this from beginning.

Headers and imports

I'll assume here, that you have a standard MFC application (such as Dialog , SDI or MDI applications). First of all you have to initialize COM, since we gonna use MSHTML COM interfaces. This can be done in InitInstance() function of your application. Remember also to uninitialize COM in your ExitInstance():

BOOL CYourApp::InitInstance()
{
	CoInitialize(NULL);
	...
}
int CYourApp::ExitInstance() 
{
	...
	CoUninitialize();
	return CWinApp::ExitInstance();
}

Now in the file you are going to use MSHTML interfaces, include mshtml.h, comdef.h (for smart pointers) and import mshtml.tlb:

#include <comdef.h>
#include <mshtml.h>
#pragma warning(disable : 4146)	//see Q231931 for explaintation
#import <mshtml.tlb> no_auto_exclude

Where do I get a document?

Now let's get a pointer to IHTMLDocument interface. How you will get it? Depends on what you already have :) If you are hosting a WebBrowser control or using CHtmlView in your application, u can call GetDocument() function in store the return value in your pointer, but I will explain how to get a 'free' document, which is not attached to any control or view. This can be done by simple call to CoCreateInstance() function:

MSHTML::IHTMLDocument2Ptr pDoc;
HRESULT hr = CoCreateInstance(CLSID_HTMLDocument, NULL, CLSCTX_INPROC_SERVER, 
                              IID_IHTMLDocument2, (void**)&pDoc);

Validate that you have a valid pointer (not NULL) and move on.

Converting your HTML code

I'll assume that you have all HTML code you want to load in some variable called lpszHTMLCode. This can be CString or any other buffer, loaded for example from file on disk. We need to prepare it before passing to MSHTML. The problem is that MSHTML function we are going to use takes only SAFEARRAY as parameter. So let's convert our string to SAFEARRAY:

SAFEARRAY* psa = SafeArrayCreateVector(VT_VARIANT, 0, 1);
VARIANT *param;
bstr_t bsData = (LPCTSTR)lpszHTMLCode;
hr =  SafeArrayAccessData(psa, (LPVOID*)&param);
param->vt = VT_BSTR;
param->bstrVal = (BSTR)bsData;

Last jump

Now we are ready to pass our SAFEARRAY to write() function. These 2 lines of code will do all dirty parsing work for you

hr = pDoc->write(psa);	//write your buffer
hr = pDoc->close();	//and closes the document, "applying" your code  

//Don't forget to free the SAFEARRAY!
SafeArrayDestroy(psa);

Of course, remember to check every your step, so your program never crush, I skipped it to keep the code simple.

Now, after all this work you have a pointer to IHTMLDocument2 interface, which gives you a lot of features, like getting particular tag, searching, inserting, replacing, deleting tag, just like you do it in JavaScript.

And remember, if you are using smart pointers (like I do here) you don't need to call Release() function, the object will be freed automatically.

"about:blank" bug workaround

Well, since we have no site "attached" to our document interface, all links (href, src) that are relative to document, will start with "about:blank" if you'll try to use IHTMLAnchorElement::href property. The way to get the exact link, as it is in HTML source, is to use IHTMLElement interface with nice function called getAttribute. Just remember that the second parameter of this function should be 2, it will tell to parser to return you text as is.

Of course same way you should work with IMG, LINK and other tags. The example project updated with this fix also. You can download it and see how I did it.

References

Ahser Kobin's article about parsing with IMarkupServices (CodeGuru)
Load HTML from Stream (MSDN)
MSHTML Reference (MSDN)
IHTMLDocument2 Reference (MSDN)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader Varonis
Israel Israel
I was born in small town Penza, Russia, in October 13th, 1975 yr. So my mother tongue is Russian. I finished the school there and learned in University, then I came to Israel and since then, I live there (or here *s*)
My profession is a C++ programmer under MS Windows platforms, but my hobby is Web development and ASP programming.

I started interesting in computers and programming somewere in 1990-1991 yrs., when my father brought home our first computer - Sinclair ZX Spectrum (he made it by himself). So I learned Basic and joined the Basic programmers club at my school (me and my friend were the only 2 guys from all school there, lol). After I finished the school (1992yr) I decided to continue my study at University and got specialization Operation Systems and Software Engineer. Although I still like my profession, but I always wanted something new, thus I learned HTML, Javascript and ASP which turned to be my hobby Smile | :)

Comments and Discussions

 
GeneralCoverting text file to HTML file Pin
Ad_1614-Jun-09 19:41
Ad_1614-Jun-09 19:41 
GeneralIHTMLDocument is empty Pin
keshavkrity14-Oct-08 5:58
keshavkrity14-Oct-08 5:58 
Jokehelp on Microsoft WebBrowser ActiveX Pin
rupert_durans20-Nov-07 1:44
rupert_durans20-Nov-07 1:44 
GeneralRe: help on Microsoft WebBrowser ActiveX Pin
Philip Patrick20-Nov-07 2:33
professionalPhilip Patrick20-Nov-07 2:33 
GeneralIHTMLDocument2::write documentation Pin
Sam Hobbs23-Jul-07 7:20
Sam Hobbs23-Jul-07 7:20 
AnswerRe: IHTMLDocument2::write documentation Pin
Philip Patrick23-Jul-07 8:39
professionalPhilip Patrick23-Jul-07 8:39 
GeneralIHTMLElement Iterator Pin
Jeffrey Walton24-Dec-06 6:52
Jeffrey Walton24-Dec-06 6:52 
GeneralRun-time error... Pin
shertay19-Jan-06 15:03
shertay19-Jan-06 15:03 
GeneralBugfix for memory corruption Pin
Pete6514-Nov-05 13:36
Pete6514-Nov-05 13:36 
Generalwhen call pdoc-&gt;write, the CPU reaches 100% and not return Pin
Anonymous4-Aug-05 21:45
Anonymous4-Aug-05 21:45 
GeneralRe: when call pdoc-&gt;write, the CPU reaches 100% and not return Pin
dchris_med23-Oct-05 19:03
dchris_med23-Oct-05 19:03 
GeneralRe: when call pdoc-&gt;write, the CPU reaches 100% and not return Pin
Anonymous26-Oct-05 23:42
Anonymous26-Oct-05 23:42 
GeneralCan't load mht files!! Pin
Tcpip20059-Apr-05 22:42
Tcpip20059-Apr-05 22:42 
Questionhow to loading mht from a Stream Pin
riverclod9-Apr-05 22:41
riverclod9-Apr-05 22:41 
GeneralMemory Leak Pin
ronald shan29-Mar-05 12:03
ronald shan29-Mar-05 12:03 
GeneralRe: Memory Leak Pin
Hewllet2-Aug-05 7:27
Hewllet2-Aug-05 7:27 
GeneralRe: Memory Leak Pin
kenkw1-Jan-06 18:50
kenkw1-Jan-06 18:50 
The following function needs to be added

SafeArrayUnaccessData(psa);

before

SafeArrayDestroy(psa);

Let me know if this works. Thanks.

GeneralRe: Memory Leak Pin
cristip325-Feb-06 1:22
cristip325-Feb-06 1:22 
GeneralRe: Memory Leak Pin
cristip325-Feb-06 2:14
cristip325-Feb-06 2:14 
GeneralRe: Memory Leak Pin
swapnil gulhane28-May-09 20:24
swapnil gulhane28-May-09 20:24 
GeneralRe: Memory Leak Pin
Member 376114822-Feb-12 7:26
Member 376114822-Feb-12 7:26 
GeneralRe: Memory Leak Pin
k0walski2k17-Jun-22 5:41
k0walski2k17-Jun-22 5:41 
GeneralSmall fix Pin
dchris_med1-Jan-05 3:10
dchris_med1-Jan-05 3:10 
QuestionRe: Small fix Pin
fehu8-May-06 20:41
fehu8-May-06 20:41 
GeneralRe: Small fix Pin
Jeffrey Walton24-Dec-06 3:51
Jeffrey Walton24-Dec-06 3:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.