IMarkupServices::ParseString Method

Creates an IMarkupContainer that contains the results of parsing the contents of a string.

Syntax

HRESULT ParseString(
    OLECHAR *pchHTML,
    DWORD dwFlags,
    IMarkupContainer **ppContainerResult,
    IMarkupPointer *ppPointerStart,
    IMarkupPointer *ppPointerFinish
);

Parameters

  • pchHTML
    [in] Value of type OLECHAR that specifies the NULL-terminated string to parse.
  • dwFlags
    [in] Unsigned integer value that specifies the following flag, or zero.
    • PARSE_ABSOLUTIFYIE40URLS
      Combines relative URLs with the base URL of the markup container.
  • ppContainerResult
    [out] Address of a pointer to an IMarkupContainer interface that returns the results of parsing the contents of a string.
  • ppPointerStart
    [in] Pointer to an IMarkupPointer interface used to indicate the start point of the selection in the string.
  • ppPointerFinish
    [in] Pointer to an IMarkupPointer interface used to indicate the end point of the selection in the string.

Return Value

Returns S_OK if successful, or an error value otherwise.

Examples

The following function parses a string of HTML into a IHTMLDocument3 object that can be manipulated like a document fragment. The code first creates an empty HTML document that is used as the target of the new HTML. The document is initialized, and an insertion point is created. After the string is parsed, the IMarkupContainer points to the new document fragement. Because the root document contains the objects created by the parse, it cannot be released until after the new fragment is no longer needed. The following code invokes a callback function to perform the user-defined action.

#include <mshtml.h>

typedef HRESULT (MarkupCallback)(IHTMLDocument3*);

HRESULT TestMarkupServices(BSTR bstrHtml, MarkupCallback* pCallback)
{
    IHTMLDocument3* pHtmlDocRoot = NULL;

    // Create the root document -- a "workspace" for parsing.
    HRESULT hr = CoCreateInstance(CLSID_HTMLDocument, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pHtmlDocRoot));
    if (SUCCEEDED(hr) && pHtmlDocRoot)
    {
        IPersistStreamInit *pPersistStreamInit = NULL;

        HRESULT hr = pHtmlDocRoot->QueryInterface(IID_PPV_ARGS(&pPersistStreamInit));
        if (SUCCEEDED(hr))
        {
            // Initialize the root document to a default state -- ready for parsing.
            hr = pPersistStreamInit->InitNew();

            IMarkupServices *pMarkupServices = NULL;
            hr = pHtmlDocRoot->QueryInterface(IID_PPV_ARGS(&pMarkupServices));
            if (SUCCEEDED(hr))
            {
                IMarkupPointer *pMarkupBegin       = NULL;
                IMarkupPointer *pMarkupEnd         = NULL;

                // These markup pointers indicate the insertion point.
                hr = pMarkupServices->CreateMarkupPointer(&pMarkupBegin);
                if (SUCCEEDED(hr))
                    hr = pMarkupServices->CreateMarkupPointer(&pMarkupEnd);

                if (SUCCEEDED(hr) && pMarkupBegin && pMarkupEnd)
                {
                    IMarkupContainer *pMarkupContainer = NULL;

                    // Parse the string -- the markup container contains the parsed HTML.
                    // Markup pointers are updated to point to begining and end of new container.
                    hr = pMarkupServices->ParseString(bstrHtml, 0, &pMarkupContainer, pMarkupBegin, pMarkupEnd);
                    if (SUCCEEDED(hr) && pMarkupContainer)
                    {
                        IHTMLDocument3 *pHtmlDoc = NULL;

                        // Retrieve the document interface to the markup container.
                        hr = pMarkupContainer->QueryInterface(IID_PPV_ARGS(&pHtmlDoc));
                        if (SUCCEEDED(hr) && pHtmlDoc)
                        {
                            // Invoke the user-defined action for this new fragment.
                            hr = pCallback(pHtmlDoc);

                            // Clean up.
                            pHtmlDoc->Release();
                        }
                        pMarkupContainer->Release();
                    }
                    pMarkupEnd->Release();
                }
                if (pMarkupBegin)
                    pMarkupBegin->Release();
                pMarkupServices->Release();
            }
            pPersistStreamInit->Release();
        }
        pHtmlDocRoot->Release();
    }
    return hr;
} 

The following code calls the precedingroutine in a loop to test speed and memory consumption. The TestDocumentText demonstrates various possibilities by getting the document body, the document element (html), or a specific element by id.

HRESULT TestDocumentText(IHTMLDocument3* pHtmlDoc)
{
    IHTMLDocument2 *pDoc = NULL;
    IHTMLElement *pElem = NULL;
    BSTR bstrId = SysAllocString(L"test");

    HRESULT hr = pHtmlDoc->QueryInterface(IID_PPV_ARGS(&pDoc));
    if (SUCCEEDED(hr) && pDoc)
    {
        hr = pDoc->get_body(&pElem);
        // hr = pHtmlDoc->get_documentElement(&pElem);
        // hr = pHtmlDoc->getElementById(bstrId, &pElem);
        if (SUCCEEDED(hr) && pElem)
        {
            BSTR bstrText = NULL;
            pElem->get_innerHTML(&bstrText);
            SysFreeString(bstrText);
            pElem->Release();
        }
        
        pDoc->Release();
    }

    SysFreeString(bstrId);
    return hr;
}
 
int _tmain(int argc, _TCHAR* argv[])
{
    CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);

    // BSTR bstrHtml = SysAllocString(L"<p>Hello World!</p>");
    BSTR bstrHtml = SysAllocString(L"<HTML><BODY><TABLE><TR><TD id=\"test\">Hello World!</TD></TR></TABLE></BODY></HTML>");

    for (int i=0; i<10000; i++) {
        HRESULT hr = TestMarkupServices(bstrHtml, &TestDocumentText);
    }

    SysFreeString(bstrHtml);

    CoUninitialize();
} 

See Also

IMarkupServices::ParseGlobal