You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

libxml2 2.9.10:如何用API解码xmlNodeGetContent获取的Base64数据?

解决libxml2 2.9.10中Base64解码的问题

在libxml2 2.9.10版本中,xmlBase64Decode属于内部未导出的函数,不能直接调用。你应该使用公开的xmlBase64DecodeBuffer接口来完成Base64数据的解码,它定义在libxml/base64.h头文件中。

具体实现步骤:

  1. 确保包含对应的头文件:
#include <libxml/base64.h>
  1. 解码示例代码:
// 你已通过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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 18:55:34