如何让libxml2不显示连接错误?
How to make libxml2 not display connection errors?
考虑以下代码:
#include <stdio.h>
#include <libxml/parser.h>
int main(void) {
xmlDocPtr doc;
xmlChar *s;
doc = xmlParseFile("http://localhost:8000/sitemap.xml");
s = xmlNodeGetContent((struct _xmlNode *)doc);
printf("%s\n", s);
return 0;
}
输出:
$ gcc -g3 -O0 $(xml2-config --cflags --libs) 1.c
$ ./a.out
error : Operation in progress
<result of xmlNodeGetContent>
也就是说,xmlParseFile
产生了不需要的输出。这里发生的是 libxml2
尝试 translate localhost
to IP address. What it gets is ::1
and 127.0.0.1
. connect("[::1]:8000")
results in EINPROGRESS
(since libxml2
sets O_NONBLOCK
on the descriptor). So libxml2
waits for it to finish, which results in POLLOUT | POLLERR | POLLHUP
, and libxml2
reports an error.
后续 connect("127.0.0.1:8000")
调用成功,所以总的来说程序成功完成。
有没有办法避免这种额外的输出?
正如 nwellnhof 所建议的,可以通过设置错误处理程序来规避连接错误。特别是结构化的错误处理程序,不管它是什么意思。
虽然 the answer 在另一个问题中或多或少地回答了我的问题,但另一个问题是关于解析器错误的。答案没有提供示例代码。所以,
#include <stdio.h>
#include <libxml/parser.h>
void structuredErrorFunc(void *userData, xmlErrorPtr error) {
printf("xmlStructuredErrorFunc\n");
}
void genericErrorFunc(void *ctx, const char * msg, ...) {
printf("xmlGenericErrorFunc\n");
}
int main(void) {
xmlDocPtr doc;
xmlChar *s;
xmlSetGenericErrorFunc(NULL, genericErrorFunc);
xmlSetStructuredErrorFunc(NULL, structuredErrorFunc);
doc = xmlParseFile("http://localhost:8000/sitemap.xml");
s = xmlNodeGetContent((struct _xmlNode *)doc);
printf("%s\n", s);
return 0;
}
这个输出,
xmlStructuredErrorFunc
<result of xmlNodeGetContent>
考虑以下代码:
#include <stdio.h>
#include <libxml/parser.h>
int main(void) {
xmlDocPtr doc;
xmlChar *s;
doc = xmlParseFile("http://localhost:8000/sitemap.xml");
s = xmlNodeGetContent((struct _xmlNode *)doc);
printf("%s\n", s);
return 0;
}
输出:
$ gcc -g3 -O0 $(xml2-config --cflags --libs) 1.c
$ ./a.out
error : Operation in progress
<result of xmlNodeGetContent>
也就是说,xmlParseFile
产生了不需要的输出。这里发生的是 libxml2
尝试 translate localhost
to IP address. What it gets is ::1
and 127.0.0.1
. connect("[::1]:8000")
results in EINPROGRESS
(since libxml2
sets O_NONBLOCK
on the descriptor). So libxml2
waits for it to finish, which results in POLLOUT | POLLERR | POLLHUP
, and libxml2
reports an error.
后续 connect("127.0.0.1:8000")
调用成功,所以总的来说程序成功完成。
有没有办法避免这种额外的输出?
正如 nwellnhof 所建议的,可以通过设置错误处理程序来规避连接错误。特别是结构化的错误处理程序,不管它是什么意思。
虽然 the answer 在另一个问题中或多或少地回答了我的问题,但另一个问题是关于解析器错误的。答案没有提供示例代码。所以,
#include <stdio.h>
#include <libxml/parser.h>
void structuredErrorFunc(void *userData, xmlErrorPtr error) {
printf("xmlStructuredErrorFunc\n");
}
void genericErrorFunc(void *ctx, const char * msg, ...) {
printf("xmlGenericErrorFunc\n");
}
int main(void) {
xmlDocPtr doc;
xmlChar *s;
xmlSetGenericErrorFunc(NULL, genericErrorFunc);
xmlSetStructuredErrorFunc(NULL, structuredErrorFunc);
doc = xmlParseFile("http://localhost:8000/sitemap.xml");
s = xmlNodeGetContent((struct _xmlNode *)doc);
printf("%s\n", s);
return 0;
}
这个输出,
xmlStructuredErrorFunc
<result of xmlNodeGetContent>