源码下载地址:https://sourceforge.net/projects/tinyxml/
学习可参考源码xmltest.cpp
//简单示例代码
int GerXmlConfigData()
{
int count = 0;
TiXmlNode *node = 0;
TiXmlElement *todoElement = 0;
TiXmlElement *itemElement = 0;
vector<st_wifi_data> vec_tmp;
const char *wifiname = NULL;
const char *wifipwd = NULL;
const char *wifitype = NULL;
const char *wifi_info =
"<?xml version=\"1.0\" standalone='no' >\n"
"<Wifi_Info>\n"
"<Item wifi_name=\"Chayan\" wifi_pwd=\"i1234\" wifi_type=\"1\" />"
"<Item wifi_name=\"Pro\" wifi_pwd=\"99206\" wifi_type=\"1\"/>"
"</Wifi_Info>";
TiXmlDocument doc(FILE_PATH);
bool loadOkay = doc.LoadFile();
if (!loadOkay)
{
std::cout << "Could not load file 'wifi_info.xml'. Error=" << doc.ErrorDesc() << std::endl;
doc.Parse(wifi_info);
if (doc.Error())
{
std::cout << "Error in " << doc.Value() << ":" << doc.ErrorDesc() << std::endl;
return -1;
}
loadOkay = doc.SaveFile();
if (!loadOkay)
{
std::cout << "SaveFile 'wifi_info.xml' fail" << std::endl;
return -1;
}
loadOkay = doc.LoadFile();
if (!loadOkay)
{
std::cout << "Could not load file 'wifi_info.xml'. Error=" << doc.ErrorDesc() << std::endl;
return -1;
}
}
std::cout << "** Printing via operator<< **\n"
<< doc << std::endl;
node = doc.FirstChild("Wifi_Info");
assert(node);
todoElement = node->ToElement();
assert(todoElement);
for (itemElement = todoElement->FirstChildElement();
itemElement;
itemElement = itemElement->NextSiblingElement())
{
st_wifi_data st_wifidata;
count++;
wifiname = itemElement->Attribute("wifi_name");
wifipwd = itemElement->Attribute("wifi_pwd");
wifitype = itemElement->Attribute("wifi_type");
if (wifiname == NULL || wifipwd == NULL || wifitype == NULL)
{
std::cout << "xml not have the attribute,please check xml file... " << std::endl;
return -1;
}
st_wifidata.wifiname=wifiname;
st_wifidata.wifipwd=wifipwd;
st_wifidata.wifitype=wifitype;
std::cout << count << "wifi_name=" << st_wifidata.wifiname
<< ",wifi_pwd=" << st_wifidata.wifipwd
<< ",wifi_type=" << st_wifidata.wifitype << std::endl;
vec_wifi_data.push_back(st_wifidata);
}
return 0;
}
|