pugixml node_cdata、node_pcdata set_value() 无效字符
当我设置 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=
. 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.
cY
3XA+Рz
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
弄清楚了 - set_value() 想要一个以 null 结尾的字符串。
Figured it out - set_value() wants a null-terminated string.