libxml2 2.9.10:如何用API解码xmlNodeGetContent获取的Base64数据?
解决libxml2 2.9.10中Base64解码的问题
在libxml2 2.9.10版本中,xmlBase64Decode属于内部未导出的函数,不能直接调用。你应该使用公开的xmlBase64DecodeBuffer接口来完成Base64数据的解码,它定义在libxml/base64.h头文件中。
具体实现步骤:
- 确保包含对应的头文件:
#include <libxml/base64.h>
- 解码示例代码:
// 你已通过xmlNodeGetContent获取的Base64数据 xmlChar *base64_data = xmlNodeGetContent(cur); if (base64_data == NULL) { // 处理节点内容获取失败的情况 return; } int decoded_length = 0; // 调用xmlBase64DecodeBuffer解码,返回原始二进制数据,decoded_length会被设置为解码后数据的长度 unsigned char *raw_binary = xmlBase64DecodeBuffer((const char *)base64_data, xmlStrlen(base64_data), &decoded_length); if (raw_binary == NULL) { // 处理解码失败的情况 xmlFree(base64_data); return; } // 这里可以使用raw_binary,其有效长度为decoded_length // 记得释放分配的内存 xmlFree(base64_data); xmlFree(raw_binary);
补充说明:
xmlBase64DecodeBuffer会自动处理Base64字符串中合法的空白字符(比如换行、空格),无需手动预处理输入的Base64数据。- 解码完成后,务必通过
xmlFree释放base64_data和raw_binary,避免内存泄漏。
内容的提问来源于stack exchange,提问作者malat
相关产品推荐
相关产品推荐

