[webkit-help] What is the correct way to browse a DOM tree?

houzhe at sohu.com houzhe at sohu.com
Fri Oct 11 06:24:26 PDT 2013


Greeting, all, 

I try to browse a DOM tree by this way: 

After web page is loaded, I use IWebFrame to get IDOMDocument, then to get IDOMElement, then use it as the root node to browse all of the child nodes and print their name, value, and attributes. I just found value is always empty, and attributes are not accessible. Why and what is the correct way to do this? Following is key part of my codes and thanks a lot for your help in advance!

int DumpDOMTree (IDOMHTMLElementPtr domElement, int offsite = 0) 
{
    HRESULT hr = FALSE; 
    UINT len = 0; 
    if (domElement != 0)
    {
        OLECHAR a_strName[MAX_NAME_LEN] = {0};
        OLECHAR a_strValue[MAX_NAME_LEN] = {0};

        BSTR strName = a_strName;
        BSTR strValue = a_strValue; 

        domElement->nodeName(&strName);
        domElement->nodeValue(&strValue);

        WCHAR line[MAX_LINE_LEN] = {0};
        for (int i=0; i<offsite; i++)
        {
            line[i] = ' ';
        }

        // write begin tag
        wsprintf(line+wcslen(line), L"<%s", strName);

        // write attribute
        IDOMNameNodeMapPtr attrs; 
        hr = domElement->attributes(&attrs.GetInterfacePtr());

        if (!SUCCEEDED(hr))
        {
            sprintf(LOGBUF, "DumpDOMTree - attribute failed - 0");
            Logger::Instance()->WriteLog(LOGBUF);
            return -1; 
        }
        len = 0; 
        hr = attrs->length(&len);
        if (!SUCCEEDED(hr))
        {
            sprintf(LOGBUF, "DumpDOMTree - attribute failed - 1");
            Logger::Instance()->WriteLog(LOGBUF);
            return -1; 
        }
        for (int u=0; u<len; u++)
        {
            int l = wcslen(line);
            IDOMNodePtr node; 
            hr = attrs->item(u, &node.GetInterfacePtr());
            if (!SUCCEEDED(hr))
            {
                sprintf(LOGBUF, "DumpDOMTree - attribute failed - 2");
                Logger::Instance()->WriteLog(LOGBUF);
                return -1; 
            }

            OLECHAR strAttrName[MAX_NAME_LEN] = {0};
            OLECHAR strAttrValue[MAX_NAME_LEN] = {0};
            node->nodeName((BSTR*)strAttrName);
            node->nodeValue((BSTR*)strAttrValue);
            wsprintf(line+l, L" \"%s=%s\"", strAttrName, strAttrValue);
        }
        wsprintf(line+wcslen(line), L">");
	    Logger::Instance()->WriteLog(line);

        // write value
        if (wcslen(strValue) > 0)
        {
            memset(line, 0, sizeof(line));
            for (int i=0; i<offsite; i++)
            {
                line[i] = ' ';
            }
            wsprintf(line+wcslen(line), L"%s", strValue);
            Logger::Instance()->WriteLog(line);
        }

        // write child nodes
        {
            IDOMNodeListPtr nodeList; 
            domElement->childNodes(&nodeList.GetInterfacePtr());
            UINT len = 0; 
            nodeList->length(&len);
            for (int u=0; u<len; u++) 
            {
                IDOMNodePtr node; 
                nodeList->item(u, &node.GetInterfacePtr());
                DumpDOMTree(node, offsite+DOMTREE_OFFSITE_STEP);
            }
        }

        // write end tag
        memset(line, 0, sizeof(line));
        for (int i=0; i<offsite; i++)
        {
            line[i] = ' ';
        }
        wsprintf(line+wcslen(line), L"</%s>", strName);
        Logger::Instance()->WriteLog(line);
    }

    return 0; 
}

Best regards, 
Zhe




More information about the webkit-help mailing list