如何在 C++ 中使用 x XPath 表达式查询 HTML?

发布于 2024-09-26 00:25:01 字数 164 浏览 0 评论 0原文

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

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

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

I have a webbrowser and I use DocumentComplete to read the current document from the WebBrowser (as IHTMLDocument2).

What's the easiest way to run xpath queries in that html doc? I am looking for something easy to use and lightweight.

I am using Visual Studio C++ 2010.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉宸 2024-10-03 00:25:01

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

我使用的是 Visual Studio C++ 2010。

通常,无法根据 HTML 文档计算 XPath 表达式。

但是,如果 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);
}

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

What's the easiest way to run xpath
queries in that html doc? I am looking
for something easy to use and
lightweight.

I am using Visual Studio C++ 2010.

Generally, XPath expressions cannot be evaluated against HTML documents.

However, if the HTML document is also an XHTML document (which is by definition a well-formed XML document), then XPath expressions can be evaluated against it.

In particular using MS Visual C++, one can use code like this:

#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);
}

Read more about this code example here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文