如何用C语言实现解析HTML文档

html-css06

如何用C语言实现解析HTML文档,第1张

参考下面代码:

#include <stdio.h>

#include <streamhtmlparser/htmlparser.h>

int main(void)

{

unsigned int getchar_ret

htmlparser_ctx *parser = htmlparser_new()

while ((getchar_ret = getchar()) != EOF) {

char c = (char)getchar_ret

/* If we received a '$' character, we output the current tag and attribute

* * name to stdout. */

if (c == '$') {

printf("[[ ")

if (htmlparser_tag(parser))printf("tag=%s ", htmlparser_tag(parser))

if (htmlparser_attr(parser)) printf("attr=%s ", htmlparser_attr(parser))

printf("]]")

/* If we read any other character, we pass it to the parser and echo it to

* * stdout. */

} else {

htmlparser_parse_chr(parser, c)

putchar(c)

}

}

}

1."COM组建"为"COM组件"。

2.建议还是不要手动解析HTML,那个似乎太费劲了。

3.在实际运用中,我也用过一些其它的XML解析工具,比如libxml,但感觉在WINDOWS还是MSXML最方便。