http://www.seanyxie.com/tinyxml%E8%AF%BB%E5%86%99xml%E7%A4%BA%E4%BE%8B%E4%BB%A3%E7%A0%81/
TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。
DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系。
如下是一个XML片段:
<Persons>
<Person ID=”1″>
<name>周星星</name>
<age>20</age>
</Person>
<Person ID=”2″>
<name>白晶晶</name>
<age>18</age>
</Person>
</Persons>
在TinyXML中,根据XML的各种元素来定义了一些类:
TiXmlBase:整个TinyXML模型的基类。
TiXmlAttribute:对应于XML中的元素的属性。
TiXmlNode:对应于DOM结构中的节点。
TiXmlComment:对应于XML中的注释
TiXmlDeclaration:对应于XML中的申明部分,即<?versiong=”1.0″ ?>。
TiXmlDocument:对应于XML的整个文档。
TiXmlElement:对应于XML的元素。
TiXmlText:对应于XML的文字部分
TiXmlUnknown:对应于XML的未知部分。
TiXmlHandler:定义了针对XML的一些操作。
TinyXML是个解析库,主要由DOM模型类(TiXmlBase、TiXmlNode、TiXmlAttribute、TiXmlComment、TiXmlDeclaration、TiXmlElement、TiXmlText、TiXmlUnknown)和操作类(TiXmlHandler)构成。
tinyxml组件读写XML非常方便,相比boost更加轻便。
下面是xml读写的模版文件
<?xml version="1.0" encoding="utf-8" standalone="yes" ?><savemaplist> <map mapid="1" name="Dota版本 v8.w3x" filename="Dota_V7.8.w3x" md5="90498B2406383B72B29D18B4DF62DC37" /> <map mapid="2" name="Dota版本 v9.w3x" filename="Dota_V1.8.w3x" md5="91498B2406383B72B29D18B4DF62DC37" /> <map mapid="3" name="Dota版本 v11.w3x" filename="Dota_V2.8.w3x" md5="91498B2406383B72B29D18B4DF62DC37" /></savemaplist>
读写代码
void writeMapXmlData(){
//save when quit
TiXmlDocument xmlDoc;
TiXmlDeclaration Declaration("1.0","utf-8","yes");
xmlDoc.InsertEndChild(Declaration);
TiXmlNode *pNode=NULL;
TiXmlElement *pRootElm=new TiXmlElement("savemaplist");
pNode=xmlDoc.InsertEndChild(*pRootElm);
pRootElm=pNode->ToElement();
TiXmlElement *pChildElm=new TiXmlElement("map");
for(int i=0;i<m_rpgMapData_.size();++i){
std::string u8node;
pChildElm->SetAttribute("mapid",m_rpgMapData_.at(i).mapid);
common::Base_W2U8(m_rpgMapData_.at(i).showmapname,u8node);
pChildElm->SetAttribute("name",u8node.c_str());
common::Base_W2U8(m_rpgMapData_.at(i).savename,u8node);
pChildElm->SetAttribute("filename",u8node.c_str());
pChildElm->SetAttribute("md5",m_rpgMapData_.at(i).md5.c_str());
pNode=pRootElm->InsertEndChild(*pChildElm);
}
xmlDoc.SaveFile(L"config\\rpgmapd.xml");}
void initReadRpgMapXmlData(){
m_rpgMapData_.clear();
std::vector<rpgMapData>().swap(m_rpgMapData_);
//first ,read xml file
TiXmlDocument xmlStr;
if(FALSE == xmlStr.LoadFile(_TEXT("config\\rpgmapd.xml"), TIXML_ENCODING_UTF8))
{
QB_error("公告信息格式错误");
return;
}
// m_vecItem.clear();
TiXmlElement* root = xmlStr.RootElement(); //TeamNews root
if (root)
{
TiXmlElement* firstNews = root->FirstChildElement();
while(firstNews){
rpgMapData item;
std::string filename;
filename=firstNews->Attribute("mapid");
item.mapid = atoi(filename.c_str());
filename=firstNews->Attribute("name");
common::Base_U82W(filename,item.showmapname);
filename=firstNews->Attribute("filename");
common::Base_U82W(filename,item.savename);
item.md5=firstNews->Attribute("md5");
item.haddownload=false;
m_rpgMapData_.push_back(item);
firstNews=firstNews->NextSiblingElement();
}
}
}
一些所需的类型
struct rpgMapData{
int mapid;
std::string md5;
std::wstring showmapname; //显示的名称
std::wstring savename; //存盘名称
bool haddownload;
rpgMapData(){
mapid=0;
md5 = "";
showmapname = L"";
haddownload = false;
}
rpgMapData(int v):mapid(v) {
md5 = "";
showmapname = L"";
haddownload = false;
}
bool operator == (const rpgMapData& _R) const{
return mapid == _R.mapid;
}};
转载于:https://blog.51cto.com/seanyxie/1574010