[C++]使用TinyXml讀寫Xml
TiXmlNode* rootElement = xmlDoc.InsertEndChild(TiXmlElement("Person"));
rootElement
->InsertEndChild(TiXmlElement("Name"))
->InsertEndChild(TiXmlText(person->m_sName.c_str()));
rootElement
->InsertEndChild(TiXmlElement("NickName"))
->InsertEndChild(TiXmlText(person->m_sNickName.c_str()));
char buffer[256];
_itoa(person->m_nAge, buffer,10);
rootElement
->InsertEndChild(TiXmlElement("Age"))
->InsertEndChild(TiXmlText(buffer));
xmlDoc.SaveFile(file.c_str());
}
xmlDoc.LoadFile();
if(xmlDoc.ErrorId() > 0)
return;
TiXmlElement* pRootElement = xmlDoc.RootElement();
if(!pRootElement)
return;
TiXmlElement* pNode = NULL;
pNode = pRootElement->FirstChildElement("Name");
if(pNode)
{
person->m_sName = pNode->GetText();
}
pNode = pRootElement->FirstChildElement("NickName");
if(pNode)
{
person->m_sNickName = pNode->GetText();
}
pNode = pRootElement->FirstChildElement("Age");
if(pNode)
{
person->m_nAge = atoi(pNode->GetText());
}
}
#include “stdafx.h”
#include <string>
#include “tinyxml.h”
#include “tinystr.h”
using namespace std;
class Person { public: string m_sName; string m_sNickName; int m_nAge; };
void SaveXml(Person* person, string file) { TiXmlDocument xmlDoc;
TiXmlNode* rootElement = xmlDoc.InsertEndChild(TiXmlElement("Person"));
rootElement
->InsertEndChild(TiXmlElement("Name"))
->InsertEndChild(TiXmlText(person->m_sName.c_str()));
rootElement
->InsertEndChild(TiXmlElement("NickName"))
->InsertEndChild(TiXmlText(person->m_sNickName.c_str()));
char buffer[256];
_itoa(person->m_nAge, buffer,10);
rootElement
->InsertEndChild(TiXmlElement("Age"))
->InsertEndChild(TiXmlText(buffer));
xmlDoc.SaveFile(file.c_str());
}
void LoadXml(string file, Person* person) { TiXmlDocument xmlDoc(file.c_str());
xmlDoc.LoadFile();
if(xmlDoc.ErrorId() > 0)
return;
TiXmlElement* pRootElement = xmlDoc.RootElement();
if(!pRootElement)
return;
TiXmlElement* pNode = NULL;
pNode = pRootElement->FirstChildElement("Name");
if(pNode)
{
person->m_sName = pNode->GetText();
}
pNode = pRootElement->FirstChildElement("NickName");
if(pNode)
{
person->m_sNickName = pNode->GetText();
}
pNode = pRootElement->FirstChildElement("Age");
if(pNode)
{
person->m_nAge = atoi(pNode->GetText());
}
}
int _tmain(int argc, _TCHAR* argv[]) { string file = “Person.xml”;
Person Larry;
Larry.m_sName = "Larry";
Larry.m_sNickName = "蹂躪";
Larry.m_nAge = 30;
SaveXml(&Larry, file);
Person NewLarry;
LoadXml(file, &NewLarry);
printf("Name: %s
“, NewLarry.m_sName.c_str()); printf(“NickName: %s “, NewLarry.m_sNickName.c_str()); printf(“Age: %d “, NewLarry.m_nAge);
return 0;
}
xw
.WriteStartElement("Person")
.WriteElementValue("Name", person->m_sName)
.WriteElementValue("NickName", person->m_sNickName)
.WriteElementValue("Age", person->m_nAge)
.WriteEndElement()
.Close();
}
using namespace std; class XmlWriter { #pragma region Const private: #define DEFAULT_BUFFER_SIZE 512 #pragma endregion
#pragma region Var private: char m_cBuffer[DEFAULT_BUFFER_SIZE]; string _sFile; TiXmlDocument _XmlDoc; stack<TiXmlNode*> _StartNodes; #pragma endregion
#pragma region Private Property private: __declspec(property(get=Get_sFile,put=Set_sFile)) string m_sFile;
__declspec(property(get=Get_XmlDoc,put=Set_XmlDoc))
TiXmlDocument& m_XmlDoc;
__declspec(property(get=Get_StartNodes))
stack<TiXmlNode*>& m_StartNodes;
__declspec(property(get=Get_pCurrentNode,put=Set_pCurrentNode))
TiXmlNode* m_pCurrentNode;
#pragma endregion
#pragma region Constructor & DeConstructor public: XmlWriter(string file); ~XmlWriter(void); #pragma endregion
#pragma region Property Process Method private: inline string Get_sFile() { return _sFile; }
inline void Set_sFile(string value)
{
if(_sFile == value)
return;
_sFile = value;
}
inline TiXmlDocument& Get_XmlDoc()
{
return _XmlDoc;
}
inline void Set_XmlDoc(TiXmlDocument& value)
{
_XmlDoc = value;
}
inline TiXmlNode* Get_pCurrentNode()
{
if(m_StartNodes.empty())
{
return &m_XmlDoc;
}
return m_StartNodes.top();
}
inline stack<TiXmlNode*>& Get_StartNodes()
{
return _StartNodes;
}
#pragma endregion
#pragma region Protected Method protected: void Reset(); #pragma endregion
#pragma region Public Method public: XmlWriter& WriteStartElement(string localName); XmlWriter& WriteEndElement(); XmlWriter& WriteElementValue(string localName, const char* value); XmlWriter& WriteElementValue(string localName, string value); XmlWriter& WriteElementValue(string localName, int value); void Close(); #pragma endregion };
#pragma region Constructor & DeConstructor XmlWriter::XmlWriter(string file) { Reset(); m_sFile = file; }
XmlWriter::~XmlWriter(void) { Reset(); } #pragma endregion
#pragma region Protected Method void XmlWriter::Reset() { _sFile = “”; _XmlDoc.Clear(); } #pragma endregion
#pragma region Public Method XmlWriter& XmlWriter::WriteStartElement(string localName) { m_StartNodes.push(m_pCurrentNode->InsertEndChild(TiXmlElement(localName.c_str())));
return *this;
}
XmlWriter& XmlWriter::WriteEndElement() { if(!m_StartNodes.empty()) m_StartNodes.pop();
return *this;
}
XmlWriter& XmlWriter::WriteElementValue(string localName, const char* value) { m_pCurrentNode ->InsertEndChild(TiXmlElement(localName.c_str())) ->InsertEndChild(TiXmlText(value));
return *this;
}
XmlWriter& XmlWriter::WriteElementValue(string localName, string value) { return WriteElementValue(localName, value.c_str()); }
XmlWriter& XmlWriter::WriteElementValue(string localName, int value) { _itoa(value, m_cBuffer,10); return WriteElementValue(localName, m_cBuffer); }
void XmlWriter::Close() { m_XmlDoc.SaveFile(m_sFile.c_str()); Reset(); } #pragma endregion