2013-02-06 3 views
1

임 CHtmlView를 사용하는 응용 프로그램에서 작업 중입니다. 새로운 요구 사항은 클래스에서 HTML 소스를 가져 와서 특정 태그를 구문 분석하거나 (가능하다면 태그의 정보 만 가져올 수 있음) 의미합니다. 우리가 새로운 시스템을 사용하고 CHtmlView :: GetSource를 사용할 수 있다면 괜찮을 것입니다. 그러나 존재하지 않습니다.CHtmlView (Visual Studio 6)에서 HTML 소스 가져 오기

저는 온라인에서 꽤 광범위한 검색을 했었지만 대부분의 Windows 프로그래밍에 익숙하지 않았고 아직 유용한 것을 얻을 수 없었습니다.

누구나 GetSource를 사용하지 않고 CHtmlView에서 HTML을 추출하는 방법에 대한 예제가 있다면 나는 그것을보고 고맙게 생각합니다. 시도했습니다.

BSTR bstr; 
    _bstr_t * bstrContainer; 
HRESULT hr; 
IHTMLDocument2 * pDoc; 
IDispatch * pDocDisp = NULL; 
pDocDisp = this->GetHtmlDocument(); 
if (pDocDisp != NULL) { 
    hr = pDocDisp->QueryInterface (IID_IHTMLDocument2, (void**)&pDoc); 
    if (SUCCEEDED(hr)) { 
     if (pDoc->toString(&bstr) != S_OK) { 
         //error... 
     } else { 
      bstrContainer = new _bstr_t(bstr); 
      size = (bstrContainer->length()+1)*2; 
      realString = new char[size]; 
      strncpy(realString, (char*)(*bstrContainer), size); 
     } 
    } else { 
     //error 
    } 
    pDocDisp->Release(); 
} 

하지만 주로 realString에서 "[object]"를 제공합니다. 내가 말했듯이, Windows는 처음이다.

도움을 주시면 감사하겠습니다.

답변

0

HTML 도우미를 검색하려면이 도우미 함수를 CHtmlView 파생 클래스에 추가하십시오. 시스템 자원이 적을 때 com-interface가 매우 신뢰할 수 없으므로이 함수에서 반환 된 부울 상태를 확인하십시오.

/* ============================================== */ 
BOOL CTest1View::GetHtmlText(CString &strHtmlText) 
{ 
    BOOL bState = FALSE; 
    // get IDispatch interface of the active document object 
    IDispatch *pDisp = this->GetHtmlDocument(); 
    if (pDisp != NULL) 
    { // get the IHTMLDocument3 interface 
     IHTMLDocument3 *pDoc = NULL; 
     HRESULT hr = pDisp->QueryInterface(IID_IHTMLDocument3, (void**) &pDoc); 
     if (SUCCEEDED(hr)) 
     { // get root element 
      IHTMLElement *pRootElement = NULL; 
      hr = pDoc->get_documentElement(&pRootElement); 
      if (SUCCEEDED(hr)) 
      { // get html text into bstr 
       BSTR bstrHtmlText; 
       hr = pRootElement->get_outerHTML(&bstrHtmlText); 
       if (SUCCEEDED(hr)) 
       { // convert bstr to CString 
        strHtmlText = bstrHtmlText; 
        bState = TRUE; 
        SysFreeString(bstrHtmlText); 
       } 
       pRootElement->Release(); 
      } 
      pDoc->Release(); 
     } 
     pDisp->Release(); 
    } 
    return bState; 
}