Xerces-C:解析 HTML 中的 Javascript

发布于 2024-12-21 19:35:36 字数 650 浏览 8 评论 0原文

我想解析网站的元标记。为此,我使用 xerces-c。

shared_ptr<SAX2XMLReader> parser(XMLReaderFactory::createXMLReader());

//Create and set callback handler with the given callback functions
Handler handler(startElement,endElement,characters);
parser->setContentHandler(&handler);
parser->setErrorHandler(&handler);

//Parse the file with the given callback handler
parser->parse(filename.c_str());

现在有些网站上有 javascript。 javascript 脚本标签内部使用运算符 &&对于逻辑和。

Xerces-C 将此解释为实体引用(例如 &nbsp)并引发异常,因为它不知道实体引用 &&。

有没有办法将其正确地读取为文本?

或者如果没有 - 有没有办法忽略脚本标签内的所有字符?反正我不需要它们。我只是想解析元标记。

I want to parse websites for their meta tags. For this I use xerces-c.

shared_ptr<SAX2XMLReader> parser(XMLReaderFactory::createXMLReader());

//Create and set callback handler with the given callback functions
Handler handler(startElement,endElement,characters);
parser->setContentHandler(&handler);
parser->setErrorHandler(&handler);

//Parse the file with the given callback handler
parser->parse(filename.c_str());

Some websites now have javascript on it. Inside of the script tags javascript uses the operator && for logical and.

Xerces-C interprets this as entity reference (for example  ) and throws an exception, because it doesn't know the entity reference &&.

Is there a way to read this correctly as text?

Or if not - is there a way to just ignore all characters inside of script tags? I don't need them anyway. I just want to parse the meta tags.

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

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

发布评论

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

评论(1

临走之时 2024-12-28 19:35:36

基本上, html 不一定是格式良好的 xml,但您可以使用 tidy 在提供给 xml 解析器之前。

Basically, html is not necessarily well-formed xml, but you can, for instance, preprocess it with tidy before feeding to xml parser.

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