【问题标题】:How to query HTML with x XPath expression in C++?如何在 C++ 中使用 x XPath 表达式查询 HTML?
【发布时间】:2011-04-20 06:51:22
【问题描述】:

我有一个网络浏览器,我使用 DocumentComplete 从网络浏览器中读取当前文档(作为 IHTMLDocument2)。

在该 html 文档中运行 xpath 查询的最简单方法是什么?我正在寻找易于使用且重量轻的东西。

我正在使用 Visual Studio C++ 2010。

【问题讨论】:

    标签: c++ visual-c++ xpath webbrowser-control ihtmldocument2


    【解决方案1】:

    运行 xpath 的最简单方法是什么 那个 html 文档中的查询?我在看 对于易于使用的东西和 轻量级。

    我正在使用 Visual Studio C++ 2010。

    一般来说,XPath 表达式无法针对 HTML 文档进行评估。

    但是,如果 HTML 文档也是 XHTML 文档(根据定义,它是格式良好的 XML 文档),则可以针对它评估 XPath 表达式。

    特别是使用 MS Visual C++,可以使用如下代码:

    #include <stdio.h>
    #import <msxml3.dll>
    using namespace MSXML2;
    
    void dump_com_error(_com_error &e);
    
    int main(int argc, char* argv[])
    {
      CoInitialize(NULL);
      try{
        IXMLDOMDocument2Ptr pXMLDoc = NULL;
        HRESULT hr = pXMLDoc.CreateInstance(__uuidof(DOMDocument30));
    
        // Set parser property settings
        pXMLDoc->async =  VARIANT_FALSE;
    
        // Load the sample XML file
        hr = pXMLDoc->load("hello.xsl");
    
        // If document does not load report the parse error 
        if(hr!=VARIANT_TRUE)
        {
          IXMLDOMParseErrorPtr  pError;
          pError = pXMLDoc->parseError;
          _bstr_t parseError =_bstr_t("At line ")+ _bstr_t(pError->Getline())
          + _bstr_t("\n")+ _bstr_t(pError->Getreason());
          MessageBox(NULL,parseError, "Parse Error",MB_OK);
          return 0;
        }
        // Otherwise, build node list using SelectNodes 
        // and returns its length as console output
        else
          pXMLDoc->setProperty("SelectionLanguage", "XPath");
          // Set the selection namespace URI if the nodes
          // you wish to select later use a namespace prefix
          pXMLDoc->setProperty("SelectionNamespaces",
          "xmlns:xsl='http://www.w3.org/1999/XSL/Transform'");
          IXMLDOMElementPtr pXMLDocElement = NULL;
          pXMLDocElement = pXMLDoc->documentElement;
          IXMLDOMNodeListPtr pXMLDomNodeList = NULL;
          pXMLDomNodeList = pXMLDocElement->selectNodes("//xsl:template");
          int count = 0;
          count = pXMLDomNodeList->length;
          printf("The number of <xsl:template> nodes is %i.\n", count);
      }
      catch(_com_error &e)
      {
        dump_com_error(e);
      }
      return 0;
    }
    
    void dump_com_error(_com_error &e)
    {
      printf("Error\n");
      printf("\a\tCode = %08lx\n", e.Error());
      printf("\a\tCode meaning = %s", e.ErrorMessage());
      _bstr_t bstrSource(e.Source());
      _bstr_t bstrDescription(e.Description());
      printf("\a\tSource = %s\n", (LPCSTR) bstrSource);
      printf("\a\tDescription = %s\n", (LPCSTR) bstrDescription);
    }
    

    阅读有关此代码示例的更多信息here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多