C++庫(TinyXML)
什么是XML?
“當 XML(擴展標記語言)於 1998 年 2 月被引入軟件工業界時,它給整個行業帶來了一場風暴。有史以來第一次,這個世界擁有了一種用來結構化文檔和數據的通用且適應性強的格式,它不僅僅可以用於 WEB,而且可以被用於任何地方。”
XML 指擴展標記語言,是一種信息交換的標准。
<!-- a xml simple example--> <Persons> <Person ID="1"> <name>Kobe</name> <age>24</age> </Person> <Person ID="2"> <name>Michael</name> <age>23</age> </Person> </Persons>
什么是TinyXML?
當前,對xml的使用非常廣泛,讀取和設置xml配置文件是我們最常用的操作。常見C/C++ XML解析器有Tinyxml、XERCES、squashxml、xmlite、pugxml、libxml等等,這些解析器有些是支持多語言的,有些只是單純C/C++的。
TinyXML是目前非常流行的一款基於DOM模型的XML解析器,簡單易用且小巧玲瓏,非常適合存儲簡單數據,配置文件,對象序列化等數據量不是很大的操作。這個解析庫的模型通過解析XML文件,然后在內存中生成DOM模型,從而讓我們很方便的遍歷這棵XML樹。
下載安裝TinyXML
下載:https://sourceforge.net/projects/tinyxml/
安裝:解壓縮tinyXML后,將這六個文件添加到你的C++工程中,分別是tinystr.h、tinystr.cpp、tinyxml.h、tinyxml.cpp、tinyxmlerror.cpp、tinyxmlparser.cpp。在需要操作xml文件的地方,使用如下代碼,就可以引入TinyXML類庫。
#include<tinyxml> 或 #include "tinyxml.h"
TinyXML類結構
TiXmlBase:整個TinyXML模型的基類。
TiXmlAttribute:對應於XML中的元素的屬性。
TiXmlNode:對應於DOM結構中的節點。
TiXmlComment:對應於XML中的注釋
TiXmlDeclaration:對應於XML中的申明部分,即<?versiong="1.0" ?>。
TiXmlDocument:對應於XML的整個文檔。
TiXmlElement:對應於XML的元素。
TiXmlText:對應於XML的文字部分
TiXmlUnknown:對應於XML的未知部分。
TiXmlHandler:定義了針對XML的一些操作。
一個XML讀寫例子
1 #include <iostream> 2 #include "tinyxml.h" 3 #include "tinystr.h" 4 #include <string> 5 #include <windows.h> 6 #include <atlstr.h> 7 8 using namespace std; 9 10 CString GetAppPath() 11 { 12 TCHAR modulePath[MAX_PATH]; 13 GetModuleFileName(NULL, modulePath, MAX_PATH); 14 CString strModulePath(modulePath); 15 strModulePath = strModulePath.Left(strModulePath.ReverseFind(_T('\\'))); 16 return strModulePath; 17 } 18 19 bool CreateXmlFile(string& szFileName) 20 { 21 try 22 { 23 TiXmlDocument *myDocument = new TiXmlDocument(); 24 TiXmlElement *RootElement = new TiXmlElement("Persons"); 25 myDocument->LinkEndChild(RootElement); 26 TiXmlElement *PersonElement = new TiXmlElement("Person"); 27 RootElement->LinkEndChild(PersonElement); 28 PersonElement->SetAttribute("ID", "1"); 29 TiXmlElement *NameElement = new TiXmlElement("name"); 30 TiXmlElement *AgeElement = new TiXmlElement("age"); 31 PersonElement->LinkEndChild(NameElement); 32 PersonElement->LinkEndChild(AgeElement); 33 TiXmlText *NameContent = new TiXmlText("Michael"); 34 TiXmlText *AgeContent = new TiXmlText("23"); 35 NameElement->LinkEndChild(NameContent); 36 AgeElement->LinkEndChild(AgeContent); 37 CString appPath = GetAppPath(); 38 string seperator = "\\"; 39 string fullPath = appPath.GetBuffer(0) +seperator+szFileName; 40 myDocument->SaveFile(fullPath.c_str()); 41 } 42 catch (string& e) 43 { 44 return false; 45 } 46 return true; 47 } 48 49 bool ReadXmlFile(string& szFileName) 50 { 51 try 52 { 53 CString appPath = GetAppPath(); 54 string seperator = "\\"; 55 string fullPath = appPath.GetBuffer(0) +seperator+szFileName; 56 TiXmlDocument *myDocument = new TiXmlDocument(fullPath.c_str()); 57 myDocument->LoadFile(); 58 TiXmlElement *RootElement = myDocument->RootElement(); 59 cout << RootElement->Value() << endl; 60 TiXmlElement *FirstPerson = RootElement->FirstChildElement(); 61 TiXmlElement *NameElement = FirstPerson->FirstChildElement(); 62 TiXmlElement *AgeElement = NameElement->NextSiblingElement(); 63 TiXmlAttribute *IDAttribute = FirstPerson->FirstAttribute(); 64 cout << NameElement->FirstChild()->Value() << endl; 65 cout << AgeElement->FirstChild()->Value() << endl; 66 cout << IDAttribute->Value()<< endl; 67 } 68 catch (string& e) 69 { 70 return false; 71 } 72 return true; 73 } 74 75 int main(void) 76 { 77 string fileName = "info.xml"; 78 CreateXmlFile(fileName); 79 ReadXmlFile(fileName); 80 }