🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.internet.web
Article #238

Extracting all the HTMLElements from IHTMLDocument2 ?

#238
From: Mischa Simmonds
Date: Mon, 13 Aug 2007 13:51
92 lines
4742 bytes
I am trying to get all the HTMLElements from IHTMLDocument2 via a 
IHTMLElementCollection that has been retrieved by All() (or in the case 
of C++, get_all(). The end result is that I am trying to access all the 
parameters of each element in the document eg: <INPUT type=hidden 
value=45533 name=Booking_No> so I can build a list containing all the 
name=value pairs.

Any ideas?


My existing C++ code which works so far in my application will geive me 
the URL, HTML, Text etc of the document....

         case DISPID_DOCUMENTCOMPLETE :                 
//DocumentComplete      DISPID( 259)
         {
            Shdocvw::tagREADYSTATE eREADYSTATE ;
            Server->Get_ReadyState( eREADYSTATE);
            if ( eREADYSTATE == Shdocvw::READYSTATE_COMPLETE) //  = 4
            {
                DelphiInterface<IHTMLDocument2> Doc = NULL;
                
if(SUCCEEDED(Server->Document->QueryInterface(IID_IHTMLDocument2,(LPVOID*)&Doc)))
                {
                    WideString wsHTML;
                    WideString wsOuterHTML;

                    WideString wsText;
                    WideString wsURL;
                    AnsiString asHTML;
                    AnsiString asText;
                    AnsiString asURL;

                    Doc->get_body(&body);
                    Doc->get_URL(&wsURL);
/*******************************************************************************************
*** BUT HERE IS MY LAME ATEMPT AT GETTING THE IHTMLElements from 
theIHTMLElementCollection ***
********************************************************************************************
                    DelphiInterface<IHTMLElementCollection> *collection;
                    Doc->get_all(&collection);
                    long p=collection->get_length(&p);
                    for(int i=0; i<p;i++)
                    {
                        element=collection-> item(i, '') as IHTMLElement;
                        DelphiInterface<IHTMLElement> element;
                        //plus I want to be able to extract the 
name/value pairs for each element
                    }
********************************************************************************************/
                    body->get_outerHTML(&wsHTML);
                    asHTML = wsHTML.Detach();
                    body->get_innerText(&wsText);
                    asText = wsText.Detach();
                    asURL = wsURL.Detach();


                    if(asHTML != "" && asURL!="")
                        if (FOnDocumentComplete) FOnDocumentComplete 
(asURL,asHTML,asText,body,Server);

                }
            }
         }


--------------------------------------------------------------------------------------------------------------------------------------------
This is an example of Delphi code I found on the web which looks close 
to be providing what I am looking for but I am unable to convert the 
Delphi to C++ in this instance...

procedure TForm.WebBrowser1DocumentComplete(Sender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
 var HTMLDocument: IHTMLDocument2;
     HTMLElementCollection: IHTMLElementCollection;
     HTMLElement: IHTMLElement;
     i: Integer;
begin
 HTMLDocument:=WebBrowser1.Document as IHTMLDocument2;
 if HTMLDocument= nil then
    raise Exception.Create('Couldn''t convert the ' +
      'FInternetExplorer.Document to an IHTMLDocument2');
  // First, grab all the elements on the web page
  HTMLElementCollection:=HTMLDocument.all;
  for i:=0 to HTMLElementCollection.length-1 do
  begin
    // Get the current element
    HtmlElement:=HTMLElementCollection.item(i, '') as IHTMLElement;
    // Add to my list of element
    .....
  end;
end
Technical Details
Message-ID: <46c061ea$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!proxad.net!feeder1-2.proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: feeec,26c174002c4041d7,start
X-Google-Attributes: gidfeeec,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
Date: Mon, 13 Aug 2007 23:51:39 +1000
From: Mischa Simmonds <mischa1.BanSpam@informationdesign.BanSpam.com.au>
User-Agent: Thunderbird 1.5.0.12 (Windows/20070509)
MIME-Version: 1.0
Newsgroups: borland.public.cppbuilder.internet.web
Subject: Extracting all the HTMLElements from IHTMLDocument2 ?
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 121.210.92.176
Message-ID: <46c061ea$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1187013098 121.210.92.176 (13 Aug 2007 06:51:38 -0700)
Lines: 93
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!proxad.net!feeder1-2.proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail