利用tinyXML实现xml文件的读写
转载自博客”C++中XML的读写操作(生成XML & 解析XML) - htj10 - 博客园 (cnblogs.com)“
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"
案例
#include<iostream> #include "../../../../Desktop/tinyxml_2_6_2/tinyxml/tinyxml.h" using namespace std; bool WriteXml(string); bool ReadXml(string); bool WriteXml1(string filePath); bool ReadXml1(string); int main() { /* bool flag= WriteXml("D://Mytextxml2.xml"); if (flag) { cout << "xml保存成功"; }*/ /*WriteXml1("D://Mytextxml3.xml"); ReadXml("D://Mytextxml2.xml");*/ ReadXml1("D://Mytextxml2.xml"); return 0; } bool WriteXml(string filePath) { try { /* <Persons> <Person ID="1" other="info"> <name>Michael</name> <age>23</age> </Person> </Persons> */ TiXmlDocument* myDocument = new TiXmlDocument(); TiXmlElement* RootElement = new TiXmlElement("Persons"); myDocument->LinkEndChild(RootElement); TiXmlElement* PersonElement = new TiXmlElement("Person"); RootElement->LinkEndChild(PersonElement); PersonElement->SetAttribute("ID", "1"); PersonElement->SetAttribute("other", "info"); TiXmlElement* NameElement = new TiXmlElement("name"); TiXmlElement* AgeElement = new TiXmlElement("age"); PersonElement->LinkEndChild(NameElement); PersonElement->LinkEndChild(AgeElement); TiXmlText* NameContent = new TiXmlText("Michael"); TiXmlText* AgeContent = new TiXmlText("23"); NameElement->LinkEndChild(NameContent); AgeElement->LinkEndChild(AgeContent); myDocument->SaveFile(filePath.c_str()); delete myDocument; } catch (string& e) { return false; } return true; } bool ReadXml(string filePath) { try { TiXmlDocument *myDocument = new TiXmlDocument(filePath.c_str());//这样最后要delete myDocument myDocument->LoadFile(); //TiXmlDocument doc; //doc.LoadFile("info.xml"); //string str("<Persons>\ // <Person ID = '1' other = 'info'>\ // <name>Michael</name>\ // <age>23</age>\ // </Person>\ // </Persons>"); //doc.Parse(str.c_str());//解析xml字符串 /*TiXmlElement* RootElement = doc.RootElement();*/ TiXmlElement* RootElement = myDocument->RootElement(); string root = RootElement->Value(); TiXmlElement* FirstPerson = RootElement->FirstChildElement(); TiXmlElement* NameElement = FirstPerson->FirstChildElement(); TiXmlElement* AgeElement = NameElement->NextSiblingElement(); TiXmlAttribute* IDAttribute = FirstPerson->FirstAttribute(); string name = NameElement->FirstChild()->Value();//或NameElement->GetText() cout << name<<endl;//Michael string age = AgeElement->FirstChild()->Value();//或AgeElement->GetText() cout << age << endl;//23 string attrName = IDAttribute->Name(); //"ID" cout << attrName << endl; string attrValue = IDAttribute->Value(); //"1" cout << attrValue << endl; attrName = IDAttribute->Next()->Name(); //"other" cout << attrName << endl; attrValue = IDAttribute->Next()->Value();//"info" cout << attrValue << endl; //delete myDocument; } catch (string& e) { return false; } return true; } bool WriteXml1(string filePath) { try { /* <?xml version="1.0" encoding="utf-8" ?> <!--this is a comment.--> <books> <book category="children" language="English" name="123English"> <title>Harry Potter</title> <anthor>J.K. Rowling</anthor> <price>29.99</price> </book> <function> <![CDATA[bool cmp(int a,int b) {return a<b;}]]> </function> </books> */ TiXmlDocument* doc = new TiXmlDocument; TiXmlDeclaration* decl = new TiXmlDeclaration("1.0", "utf-8", ""); doc->LinkEndChild(decl); TiXmlComment* pComment = new TiXmlComment("this is a comment.");//构建一条注释 doc->LinkEndChild(pComment); TiXmlElement* root, * elem, * child; TiXmlText* text; root = new TiXmlElement("books"); doc->LinkEndChild(root); elem = new TiXmlElement("book"); elem->SetAttribute("category", "children"); elem->SetAttribute("language", "English"); elem->SetAttribute("name", "123English");//可以解出来123,但“English123”不能 root->LinkEndChild(elem); child = new TiXmlElement("title"); text = new TiXmlText("Harry Potter"); child->LinkEndChild(text); elem->LinkEndChild(child); child = new TiXmlElement("anthor"); text = new TiXmlText("J.K. Rowling"); child->LinkEndChild(text); elem->LinkEndChild(child); child = new TiXmlElement("price"); text = new TiXmlText("29.99"); child->LinkEndChild(text); elem->LinkEndChild(child); //一个CDATA(Tixml看做是TiXmlText) elem = new TiXmlElement("function"); TiXmlText* pCDATA = new TiXmlText("bool cmp(int a,int b)\n{return a<b;}"); pCDATA->SetCDATA(true); elem->LinkEndChild(pCDATA); root->LinkEndChild(elem); //doc->Print();//打印到 stdout cout doc->SaveFile("D:\\test.xml");//输出到文件 TiXmlPrinter printer; //printer.SetStreamPrinting();//有这句,无空白符,就一行字符串 doc->Accept(&printer); string sRet = printer.CStr();//输出到字符串 delete doc;//只需要这一个delete,它的所有子节点都delete了 } catch (std::string& e) { return false; } } bool ReadXml1(string filePath) { //解析 //string sXml = sRet; TiXmlDocument doc; ////解析xml字符串 //doc.Parse(sXml.c_str()); //if (doc.Error()) // return false; //解析xml文件 if (!doc.LoadFile("D:\\test.xml")) return false; TiXmlElement* root = doc.RootElement(); TiXmlElement* elem = root->FirstChildElement(); //遍历 for (TiXmlElement* tmp = elem->FirstChildElement(); tmp; tmp = tmp->NextSiblingElement()) { string s = tmp->Value(); cout << s << endl;; const char* p = tmp->GetText(); string t(p ? p : ""); cout << t << endl; } //查找特定name的元素 TiXmlElement* elem2 = root->FirstChildElement("book"); //遍历Attribute for (TiXmlAttribute* pAttr = elem2->FirstAttribute(); pAttr; pAttr = pAttr->Next()) { string sName = pAttr->Name(); cout << sName << endl; string sValue = pAttr->Value(); cout << sValue << endl; } //获取特定attribute const char* psz1 = elem2->Attribute("language");//"English" const char* psz2 = elem2->Attribute("category");//"children",若没有找到"category"属性,则返回NULL cout << psz1 << "--" << psz2 << endl; //还有,直接取值的 int i; elem2->Attribute("name", &i);//返回const char* , 失败返回NULL cout << i << endl;//输出123 double d; elem2->Attribute("name", &d); cout << d << endl; elem2->QueryIntAttribute("name", &i); cout << i << endl; elem2->QueryDoubleAttribute("name", &d); cout << d << endl; bool b; elem2->QueryBoolAttribute("name", &b); cout << b << endl; //... //获取CDATA TiXmlElement* elem3 = root->FirstChildElement("function"); string ss = elem3->GetText(); string ss2 = elem3->FirstChild()->Value();//或者 }