UTF-8和TinyXML [英] UTF-8 and TinyXML
问题描述
由于某种原因,我无法正确地从xml文件中读取数据.例如,我得到的不是Schrüder",而是Schrüder".
我的代码:
tinyxml2 :: XMLDocument doc;布尔打开(字符串路径){if(doc.LoadFile(path.c_str())== XML_SUCCESS)返回true;返回false;}int main(){if(open("C:\\ Users \\ Admin \\ Desktop \\ Test.xml"))cout<<成功"<<恩德尔XMLNode *节点= doc.RootElement();字符串测试= node-> FirstChild()-> GetText();cout<<测试<<恩德尔返回0;}
XML的一部分:
<?xml version ="1.0" encoding ="UTF-8"?>< myXML>< my:TXT_UTF8Test>Schrüder</my:TXT_UTF8Test></myXML>
请注意,如果我将其转换为ANSI并将编码类型更改为"ISO-8859-15",则可以正常工作.
我读到类似"LoadFile(filename,TIXML_ENCODING_UTF8)"之类的东西应该会有所帮助.但是事实并非如此(错误:无效的参数,只需要一个const char).我有最新版本的TinyXML2(我猜是?).我是在几分钟前从 https://github.com/leethomason/tinyxml2 下载的.>
有什么想法吗?
编辑:当我将字符串写入.xml或.txt文件时,它可以正常工作.eclipse ide控制台可能存在一些问题.无论如何,当我尝试通过电子邮件发送字符串时,我也遇到同样的问题.这是MailSend脚本:
bool sendMail(std :: string params){if((int)ShellExecute(NULL,"open","H:\\ MailSend \\ MailSend_anhang.exe",params.c_str(),NULL,SW_HIDE)< = 32)返回false;返回true;
}
我在主要方法中这样调用它:
sendMail(-f:d.nitschmann@example.com -t:person2@example.com -s:Subject -b:Body" + test);
我认为问题出在您的终端上;您可以尝试在其他终端上运行测试代码吗?一种已知具有良好的UTF-8支持?
使用UTF-8模式的终端输出:
$ ./a.out成功施吕德
使用ISO-8859-15模式的终端输出:
$ ./a.out成功施罗德
也-请尝试并遵循 http://sscce.org/-为了后代,这是您的代码编译所需的一切(17676169.cpp):
#include< tinyxml2.h>#include< string>#include< iostream>使用命名空间std;使用名称空间tinyxml2;tinyxml2 :: XMLDocument doc;布尔打开(字符串路径){if(doc.LoadFile(path.c_str())== XML_SUCCESS)返回true;返回false;}int main(){if(open("Test.xml"))cout<<成功"<<恩德尔XMLNode *节点= doc.RootElement();字符串测试= node-> FirstChildElement()-> GetText();cout<<测试<<恩德尔返回0;}
编译为:
<代码> g ++ -o 17676169 17676169.cpp -ltinyxml2
和uuencoded Test.xml-确保使用完全相同的数据
开始660 Test.xmlM/#] X; 6P @ = F5R< VEO; CTB,2XP(B!E; F-O9& EN9STB551& + 3 @ B/SX */&UY6 $ U,M/@ H @(`@/&UU.E185%] 55 $ 8X5& 5S =#Y38VARP [QD97(\ + VUY.E185%] 55 $ 8X/5& 5S =#X */] M> 5A-3#X *`结尾
如果您想确认这一理论,请在Eclipse中运行:
#include< iostream>#include< string>#include< fstream>int main(){std :: ifstream ifs("Test.xml");std :: string xml_data((std :: istreambuf_iterator< char>(ifs)),std :: istreambuf_iterator< char>());std :: cout<<xml_data;}
使用UTF-8模式的终端输出:
$ ./17676169.cat<?xml version ="1.0" encoding ="UTF-8"?>< myXML>< my:TXT_UTF8Test>Schrüder</my:TXT_UTF8Test></myXML>
使用ISO-8859-15模式的终端输出:
$ ./17676169.cat<?xml version ="1.0" encoding ="UTF-8"?>< myXML>< my:TXT_UTF8Test>SchrŒder</my:TXT_UTF8Test></myXML>
For some reason I can not read data from a xml file properly. For example instead of "Schrüder" I get something like "Schrüder".
My code:
tinyxml2::XMLDocument doc;
bool open(string path) {
if(doc.LoadFile(path.c_str()) == XML_SUCCESS)
return true;
return false;
}
int main() {
if(open("C:\\Users\\Admin\\Desktop\\Test.xml"))
cout << "Success" << endl;
XMLNode * node = doc.RootElement();
string test = node->FirstChild()->GetText();
cout << test << endl;
return 0;
}
Part of XML:
<?xml version="1.0" encoding="UTF-8"?>
<myXML>
<my:TXT_UTF8Test>Schrüder</my:TXT_UTF8Test>
</myXML>
Notice that if I convert it to ANSI and change the encoding type to "ISO-8859-15" it works fine.
I read that something like "LoadFile( filename, TIXML_ENCODING_UTF8 )" should help. However that's not the case (error: Invalid arguments, it just expects a const char). I have the latest version of TinyXML2 (I guess?). I downloaded it just a couple minutes ago from https://github.com/leethomason/tinyxml2.
Any ideas?
Edit: When I write the string to a .xml or .txt file it works fine. There might be some problem with the eclipse ide console. Anyway, when I try to send the string via E-Mail, I also get the same problems. Here's the MailSend script:
bool sendMail(std::string params) {
if( (int) ShellExecute(NULL, "open", "H:\\MailSend\\MailSend_anhang.exe", params.c_str(), NULL, SW_HIDE) <= 32 )
return false;
return true;
}
I call it in the main method like this:
sendMail("-f:d.nitschmann@example.com -t:person2@example.com -s:Subject -b:Body " + test);
I think the problem is with your terminal; can you try run your test code in a different terminal ? one with known good UTF-8 support ?
Output with terminal in UTF-8 mode:
$ ./a.out
Success
Schrüder
Output with terminal in ISO-8859-15 mode:
$ ./a.out
Success
SchrÃŒder
Also - please try and follow http://sscce.org/ - for posterity sake here is your code with everything needed to compile (17676169.cpp):
#include <tinyxml2.h>
#include <string>
#include <iostream>
using namespace std;
using namespace tinyxml2;
tinyxml2::XMLDocument doc;
bool open(string path) {
if(doc.LoadFile(path.c_str()) == XML_SUCCESS)
return true;
return false;
}
int main() {
if(open("Test.xml"))
cout << "Success" << endl;
XMLNode * node = doc.RootElement();
string test = node->FirstChildElement()->GetText();
cout << test << endl;
return 0;
}
compiled with:
g++ -o 17676169 17676169.cpp -ltinyxml2
and uuencoded Test.xml - to ensure exact same data is used
begin 660 Test.xml
M/#]X;6P@=F5R<VEO;CTB,2XP(B!E;F-O9&EN9STB551&+3@B/SX*/&UY6$U,
M/@H@("`@/&UY.E185%]55$8X5&5S=#Y38VARP[QD97(\+VUY.E185%]55$8X
/5&5S=#X*/"]M>5A-3#X*
`
end
Edit 1:
If you want to confirm this theory - run this in eclipse:
#include <iostream>
#include <string>
#include <fstream>
int main()
{
std::ifstream ifs("Test.xml");
std::string xml_data((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
std::cout << xml_data;
}
Output with terminal in UTF-8 mode:
$ ./17676169.cat
<?xml version="1.0" encoding="UTF-8"?>
<myXML>
<my:TXT_UTF8Test>Schrüder</my:TXT_UTF8Test>
</myXML>
Output with terminal in ISO-8859-15 mode:
$ ./17676169.cat
<?xml version="1.0" encoding="UTF-8"?>
<myXML>
<my:TXT_UTF8Test>SchrÃŒder</my:TXT_UTF8Test>
</myXML>
这篇关于UTF-8和TinyXML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!