如何使libxml2不显示连接错误? [英] How to make libxml2 not display connection errors?

查看:114
本文介绍了如何使libxml2不显示连接错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码:

#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尝试翻译 localhost到IP地址.它得到的是::1127.0.0.1. connect("[::1]:8000") 导致完成,结果为POLLOUT | POLLERR | POLLHUP,并且libxml2报告一个错误.

That is, xmlParseFile produces undesired output. What happens here is libxml2 tries to 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")调用成功,因此所有程序均成功完成.

Subsequent connect("127.0.0.1:8000") call succeeds, so all in all the program finishes successfully.

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

Is there a way to avoid this extra output?

推荐答案

如nwellnhof所建议,可以通过设置错误处理程序来规避连接错误.尤其是结构化错误处理程序,无论其含义是什么.

As suggested by nwellnhof, connection errors can be circumvented by setting error handler. Particularly, structured error handler, whatever that means.

其他问题中的答案或多或少我的问题是,另一个问题与解析器错误有关.而且答案没有提供示例代码.所以,

While the answer in the other question more or less answers my question, that other question is about parser errors. And the answer doesn't provide example code. So,

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

这篇关于如何使libxml2不显示连接错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆