pugixml node_cdata、node_pcdata set_value() 无效字符

发布于 2024-12-28 07:00:57 字数 1379 浏览 2 评论 0原文

当我设置 node_pcdata 的值时,pugixml 似乎添加了无效字符。代码如下。我想做的是对 PNG 进行 base-64 编码并通过 XML 发送。我将 base-64 编码文本写入一个文件,以便与 pugixml 分开检查,它看起来不错,以这些有效字符 4AAAAASUVORK5CYII= 结尾。但 ndAvatarData.set_value(avatarEnc) 调用为同一运行添加了无效字符 4AAAAASUVORK5CYII= CY 3XA+Рz。有时结果是有效的,但我无法重复这一点,所以我不知道在这种情况下 pugi 输出是什么样的。

我需要在这里做些什么吗?这是 pugixml 的已知问题吗?我进行了搜索,但找不到任何讨论此问题的内容。

我在 Visual C++ 2010 中得到了这个。

char* avatarEnc = NULL;
try {
    std::ifstream avatarIn("MyAvatar.png", std::ios_base::in | std::ios_base::binary);
    if (!avatarIn.is_open())
        cerr << "Could not open avatar file!";
    else {
        base64::encoder E;
        stringstream  avatarOut;
        E.encode(avatarIn, avatarOut);

        avatarOut.seekg(0, ios::end);
        int avatarEncSize = avatarOut.tellg();
        avatarOut.seekg(0, ios::beg);

        avatarEnc = new char[avatarEncSize];
        avatarOut.read(avatarEnc, avatarEncSize);

        std::ofstream tempOut("avatarEnc.txt", std::ios_base::out | std::ios_base::binary);
        tempOut.write(avatarEnc, avatarEncSize);

        xml_node ndAvatar = root.append_child("avatar");
        xml_node ndAvatarData = ndAvatar.append_child(pugi::node_pcdata);
        ndAvatarData.set_value(avatarEnc);
    }
}
catch (...) {
    cerr << "Error loading avatar.";
}

pugixml seems to be adding invalid characters when I set the value of a node_pcdata. The code is below. What I'm trying to do is base-64 encode a PNG and send it via XML. I wrote the base-64 encoded text to a file to check it separately from pugixml, and it looks good, ending with these valid characters 4AAAAASUVORK5CYII=. But the ndAvatarData.set_value(avatarEnc) call is adding invalid characters for the same run 4AAAAASUVORK5CYII=
cY
3XA+Рz
. Occasionally the result is valid, but I have not been able to repeat that, so I don't know what the pugi output looks like in that case.

Is there something I need to do here? Is this a known problem with pugixml? I searched but could not find anything discussing this problem.

I'm getting this in Visual C++ 2010.

char* avatarEnc = NULL;
try {
    std::ifstream avatarIn("MyAvatar.png", std::ios_base::in | std::ios_base::binary);
    if (!avatarIn.is_open())
        cerr << "Could not open avatar file!";
    else {
        base64::encoder E;
        stringstream  avatarOut;
        E.encode(avatarIn, avatarOut);

        avatarOut.seekg(0, ios::end);
        int avatarEncSize = avatarOut.tellg();
        avatarOut.seekg(0, ios::beg);

        avatarEnc = new char[avatarEncSize];
        avatarOut.read(avatarEnc, avatarEncSize);

        std::ofstream tempOut("avatarEnc.txt", std::ios_base::out | std::ios_base::binary);
        tempOut.write(avatarEnc, avatarEncSize);

        xml_node ndAvatar = root.append_child("avatar");
        xml_node ndAvatarData = ndAvatar.append_child(pugi::node_pcdata);
        ndAvatarData.set_value(avatarEnc);
    }
}
catch (...) {
    cerr << "Error loading avatar.";
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纵情客 2025-01-04 07:00:57

弄清楚了 - set_value() 想要一个以 null 结尾的字符串。

avatarEnc = new char[avatarEncSize+1];

avatarOut.read(avatarEnc, avatarEncSize);

avatarEnc[avatarEncSize] = 0;

Figured it out - set_value() wants a null-terminated string.

avatarEnc = new char[avatarEncSize+1];

avatarOut.read(avatarEnc, avatarEncSize);

avatarEnc[avatarEncSize] = 0;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文