Martin Speiser
June 1st, 1999, 04:24 AM
I'm trying to parse HTML pages with the help of the MSHTML control. I'm using the C++ COM interface. Now I'm at the point where I need to parse the <SELECT> tag. I'm trying to use the method get_options. This function shall return an interface pointer for a collection of OPTION elements in form of an IDispatch interface. I thought I can query an pointer to an IHTMLElementCollection interface from the IDispatch, but it returns NULL.
So, how can I access the collection?
Martin
BKoulman
December 7th, 2006, 01:50 PM
You can access the select object as IHTMLElement*. Use get_children on that object to return a collection of HTMLElements and get_innerHTM. This will be the values of the individual options. I could not figure out how to do this any other way. Here is the code excerpt:
if (strTagName=="SELECT")
{
IDispatch* pOptionsDisp2;
pElem3->get_children(&pOptionsDisp2);
IHTMLElementCollection* pElemColl2 = NULL;
hr = pOptionsDisp2->QueryInterface(IID_IHTMLElementCollection,(void **)&pElemColl2);
if (S_OK==hr)
{
long celem2;
pElemColl2->get_length(&celem2);
for(int j = 0; j < celem2; j++)
{
VARIANT varIndex2;
varIndex2.vt = VT_UINT;
varIndex2.lVal = j;
VARIANT var22;
VariantInit( &var22 );
IDispatch* pDisp4;
HRESULT hr = pElemColl2->item( varIndex2, var22, &pDisp4 );
if ( hr == S_OK )
{
IHTMLElement* pElem4;
hr = pDisp4->QueryInterface(IID_IHTMLElement,(void **)&pElem4);
if ( hr == S_OK )
{
BSTR bstrTagName4;
BSTR bstrInnerHTML;
pElem4->get_tagName(&bstrTagName4);
pElem4->get_innerHTML(&bstrInnerHTML);
CString strInnerHTML(bstrInnerHTML);
CString strTagName4(bstrTagName4);
::SysFreeString(bstrTagName4);
}
}
}
}