比如html部分文件如下
<table >
 <thead>
     <tr>  <th>代码</th> <th>名称</th> <th>时间</th> <th>数量</th> </tr>
 </thead>
  <tbody >
    <tr><th><a href="../requery.php?code=583" target="_parent" >583</a></th>
        <th><a href="../requery.php?code=583" target="_parent">工程1</a></th>
        <th>2001-04-07</th>
        <td><span class="up">110</span></td>
    </tr>
    <tr>     ..........   </tr>
  </tbody>
</table>
网页中只有一个table,这个table中有很多行,每行的内容都与上类似。
我现在通过
pCollection = pDoc3->getElementsByTagName(L"table");然后我再怎样取得table每行tr中的代码【583】、名称【工程1】、日期【2001-04-07】、数量【110】他们的文本?
我想知道如何得到table标签下的各个子标签,就如同遍历树结构一样,能够取得每个节点的子节点,父节点?
就是我想通过得到table,然后在得到他的子节点tbody, 再到子节点tr,再得到th,再得到a,然后用getinnertext()得到583这个代码,遍历完一行,我再回溯tbody,然后开始遍历新的tr?
直到遍历完table的所有行tr多谢了,最好能以代码说明。谢谢!

解决方案 »

  1.   

    IHTMLTable::get_rows(IHTMLElementCollection **p);
    IHTMLTableRow::get_cells(IHTMLElementCollection **p);
    IHTMLElementCollection::item(VARIANT name,VARIANT index,IDispatch **pdisp);
    IHTMLElement::get_innerText(BSTR *p);
      

  2.   

    能再详细点吗,就针对我上面写出的html文件。
    多谢啊
      

  3.   

    这个我可以帮你,我也正弄这个东西呢,我大体给你个简单的代码,你修改一下可能有错
    CComPtr< IDispatch > pDisp=NULL;
    CComQIPtr <IHTMLElementCollection> pColl;
    CComQIPtr<IHTMLTable> Table;
    CComQIPtr<IHTMLTableRow> Row;
    CComQIPtr <IHTMLElement> pEl;
    CComQIPtr <IHTMLElementCollection> pIRows;
    CComQIPtr <IHTMLElementCollection> pICell;
    CComQIPtr<IHTMLTableCell> pCell;
    IHTMLDocument2* pDoc2;
    CString str;
    BSTR bsStr;
    pDoc2->get_all(&pColl);
    pColl->tags(COleVariant("table"),&pDisp);
    if (pDisp==NULL) return;
    pColl.Release();
    pColl=pDisp;
    pDisp.Release();
    long lCnt=0;
    pColl->get_length(&lCnt);//得到网页里所有的表格
    for (int n=0;n<lCnt;n++)
    {
    CComVariant cvt;
    cvt=(long)n;
    pColl->item(COleVariant((long)n),cvt,&pDisp);
    Table=pDisp;
    pDisp.Release();
    long lRow=0;
    Table->get_rows(&pIRows);
    pIRows->get_length(&lRow);//得到表格里所有的行
    for (long j=0;j<lRow;j++)
    {
    pIRows->item(COleVariant((long)j),COleVariant((long)0),&pDisp);
    Row=pDisp;
    pDisp.Release();
    Row->get_cells(&pICell);
    long lCell=0;
                 pICell->get_length(&lCell);//遍历每一行里面的单元格
    for (long k=0;k<lCell;k++)
    {
         pICell->item(COleVariant((long)k),COleVariant((long)0),&pDisp);
         pEl=pDisp;
                         pCell=pDisp;
         pEl->get_outerText(&bsStr);
                         str=bsStr;//得到单元格里面的内容
                            pEl.Release();
                         pDisp.Release();
                    }
                    Row.Release();
    pICell.Release();
             }
             pIRows.Release();
    Table.Release();
    }
    pColl.Release();
      

  4.   

    IHTMLDocument2 接口你应该知道怎么获取吧,上面代码我没有给出
      

  5.   

    在此提供几个网址: (仅供参考)
    1 XMLTree.exe 是一个示例演示如何使用 MSXML DOM 接口,从 c + + 应用程序。   
    http://support.microsoft.com/kb/246230#top  2 VC使用Cup生成、解析xml资料   
    http://download.csdn.net/source/992877  
    其中参看第一个 可以对4楼和5楼说的有更加多的理解 
      

  6.   

    sorry 楼主要解析html 我看成要解析xml了
    其实方法都差不多
    希望你解决问题后 最好贴出解决方法~
      

  7.   

    好的。我试试chu102给出的方法,差不多我就结贴了。呵呵。
    管用的话,不知道可不可以加分结贴。谢谢~