如何使libxml2不显示连接错误?

请考虑以下代码:

#include  #include  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  

也就是说, xmlParseFile会产生不需要的输出。 这里发生的是libxml2尝试将 localhost 转换为IP地址。 得到的是::1127.0.0.1connect("[::1]:8000")导致EINPROGRESS (因为libxml2在描述符上设置了O_NONBLOCK )。 所以libxml2等待它完成 ,这导致POLLOUT | POLLERR | POLLHUP POLLOUT | POLLERR | POLLHUP POLLOUT | POLLERR | POLLHUPlibxml2报告错误 。

后续connect("127.0.0.1:8000")调用成功,因此所有程序都成功完成。

有没有办法避免这种额外的输出?

正如nwellnhof所建议的那样,可以通过设置error handling程序来规避连接错误。 特别是结构化error handling程序,无论这意味着什

虽然其他问题的答案或多或少地回答了我的问题,但另一个问题是解析器错误。 答案没有提供示例代码。 所以,

 #include  #include  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