声明部分: class XmlDocControl { public: XmlDocControl(); ~XmlDocControl(); void Release(); const char* GetFilePath() const; const char* GetFileName() const; public: bool ParseFile(const char* pXmlFile, const char* pFilePath = NULL); TiXmlElement* GetRootElement() const; TiXmlElement* GetNextElement(TiXmlElement* pPreElement); TiXmlElement* GetChildElement(TiXmlElement* pParentElement, const char* pElementName = NULL); const char* GetElementValue(const TiXmlElement* pElement) const; const char* GetElementAttributeValue(TiXmlElement* pElement, const char* pAttributeName); public: bool SaveXmlFile(const char* pFileName, const char* pFilePath = NULL); TiXmlElement* AddRootElement(const char* pRootName); TiXmlElement* AddChildElement(TiXmlElement* pParentElement, const char* pChildName, const char* pChildValue = NULL); bool AddDeclaration(const char* pVersion = "1.0", const char* pEnCoding = "gb2312", const char* pStandalone = "Yes"); bool AddElementValue(TiXmlElement* pElement, const char* pElementValue); bool AddElementAttribute(TiXmlElement* pElement, const char* pAttributeName, const char* pAttributeValue); bool AddComment(TiXmlElement* pElement,const char* pCommentValue); public: bool ModifyElementValue(TiXmlElement* pElement, const char* pNewValue); bool ModifyElementAttributeValue(TiXmlElement* pElement, const char* pAttributeName, const char* pNewValue); private: bool CreateMutiDirectory(const char* pXmlFile); void SetDefaultFilePath(); private: std::string m_szFileName; //Xml file name std::string m_szFilePath; //Xml file path TiXmlDocument* m_pXmlFile; //Xml document }; 定义部分: #include "XmlHelper.h" XmlDocControl::XmlDocControl() { Initialize(); } XmlDocControl::~XmlDocControl() { Release(); } bool XmlDocControl::Initialize() { m_szFileName.clear(); m_szFilePath.clear(); m_pXmlFile = new TiXmlDocument(); if (NULL == m_pXmlFile) { return false; } return true; } void XmlDocControl::Release() { if (NULL != m_pXmlFile) { delete m_pXmlFile; m_pXmlFile = NULL; } } const char* XmlDocControl::GetFilePath() const { return m_szFilePath.c_str(); } const char* XmlDocControl::GetFileName() const { return m_szFileName.c_str(); } bool XmlDocControl::CreateMutiDirectory(const char* pXmlFile) { CString sPathFile(pXmlFile); CString sTemp = ""; CArray<CString> arrayPath; //Get all folders for (int i = 0; i < sPathFile.GetLength(); i++) { if ('//' != sPathFile.GetAt(i)) { sTemp.AppendChar(sPathFile.GetAt(i)); } else { arrayPath.Add(sTemp); sTemp.AppendChar('//'); } //If the last folder if (i == sPathFile.GetLength() - 1) { arrayPath.Add(sTemp); } } //The folder should be created if it doesn't already exit for (int i = 1; i < arrayPath.GetSize(); i++) { WIN32_FIND_DATA FindFileData; bool bCreateFolder = true; HANDLE hFind = FindFirstFile(arrayPath.GetAt(i), &FindFileData); if ((INVALID_HANDLE_VALUE != hFind) && (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { bCreateFolder = false; } if (bCreateFolder) { ::CreateDirectory(arrayPath.GetAt(i), NULL); } FindClose(hFind); } return true; } void XmlDocControl::SetDefaultFilePath() { char cPath[_MAX_PATH] = _T(""); //The cPath is "C:/Documents and Settings/username/Local Settings/Application Data" SHGetSpecialFolderPath(NULL, cPath, CSIDL_LOCAL_APPDATA, TRUE); //Now it is "C:/Documents and Settings/username/Local Settings/Application Data/Skoito system/XML/pXmlFileName" std::ostringstream ostr; ostr << cPath << PATH_SKOITOSYSTEM_ALLFILE << PATH_XML_ALLFILE; m_szFilePath = ostr.str(); } bool XmlDocControl::ParseFile(const char* pFileName, const char* pFilePath) { if (NULL == pFilePath) { SetDefaultFilePath(); } else { m_szFilePath = pFilePath; } //XML file name, like "SystemConfig.xml" m_szFileName = pFileName; if (NULL != m_pXmlFile) { std::ostringstream ostr; ostr << m_szFilePath << "/" << m_szFileName; m_pXmlFile->LoadFile(ostr.str().c_str()); } if (m_pXmlFile->Error()) { int nErrorID = m_pXmlFile->ErrorId(); TRACE("%s", m_pXmlFile->ErrorDesc()); return false; } return true; } TiXmlElement* XmlDocControl::GetRootElement() const { return m_pXmlFile->RootElement(); } TiXmlElement* XmlDocControl::GetNextElement(TiXmlElement* pPreElement) { if (NULL != pPreElement) { return pPreElement->NextSiblingElement(); } return NULL; } TiXmlElement* XmlDocControl::GetChildElement(TiXmlElement* pParentElement, const char* pElementName) { if (NULL != pParentElement) { if (NULL != pElementName) { TiXmlNode * ChildNode = pParentElement->FirstChildElement(); while(ChildNode) { if (0 == strcmp(pElementName, ChildNode->Value())) { return ChildNode->ToElement(); } ChildNode = GetNextElement(ChildNode->ToElement()); } } else { return pParentElement->FirstChildElement(); } } return NULL; } const char* XmlDocControl::GetElementValue(const TiXmlElement* pElement) const { if (NULL != pElement) { return pElement->GetText(); } return NULL; } const char* XmlDocControl::GetElementAttributeValue(TiXmlElement* pElement, const char* pAttributeName) { if ((NULL != pElement) && (NULL != pAttributeName)) { return pElement->Attribute(pAttributeName); } return NULL; } bool XmlDocControl::SaveXmlFile(const char* pFileName, const char* pFilePath) { if (NULL == pFilePath) { SetDefaultFilePath(); } else { m_szFilePath = pFilePath; } //XML file name, like "SystemConfig.xml" m_szFileName = pFileName; std::ostringstream ostr; ostr << m_szFilePath << "/" << m_szFileName; return (m_pXmlFile->SaveFile(ostr.str().c_str())); } TiXmlElement* XmlDocControl::AddRootElement(const char* pRootName) { TiXmlElement* pRootElement = new TiXmlElement(pRootName); if (NULL != pRootElement) { m_pXmlFile->LinkEndChild(pRootElement); return m_pXmlFile->RootElement(); } return NULL; } TiXmlElement* XmlDocControl::AddChildElement(TiXmlElement* pParentElement, const char* pChildName, const char* pChildValue) { if (NULL != pParentElement) { TiXmlElement* pChildElement = new TiXmlElement(pChildName); pParentElement->LinkEndChild(pChildElement); if (NULL != pChildValue) { AddElementValue(pChildElement, pChildValue); } return pChildElement; } return NULL; } bool XmlDocControl::AddDeclaration(const char* pVersion, const char* pEnCoding, const char* pStandalone) { TiXmlDeclaration* pDeclaration = new TiXmlDeclaration(pVersion, pEnCoding, pStandalone); if (NULL != pDeclaration) { m_pXmlFile->LinkEndChild(pDeclaration); return true; } return false; } bool XmlDocControl::AddElementValue(TiXmlElement* pElement, const char* pElementValue) { if (NULL != pElement) { TiXmlText* pText = new TiXmlText(pElementValue); pElement->LinkEndChild(pText); return true; } return false; } bool XmlDocControl::AddElementAttribute(TiXmlElement* pElement, const char* pAttributeName, const char* pAttributeValue) { if (NULL != pElement) { pElement->SetAttribute(pAttributeName, pAttributeValue); return true; } return false; } bool XmlDocControl::AddComment(TiXmlElement* pElement,const char* pCommentValue) { if (NULL != pElement) { TiXmlComment* pComment = new TiXmlComment(pCommentValue); pElement->LinkEndChild(pComment); return true; } return false; } bool XmlDocControl::ModifyElementValue(TiXmlElement* pElement, const char* pNewValue) { if (NULL != pElement) { TiXmlNode * pOldNode = NULL; pOldNode = pElement->FirstChild(); if (NULL != pOldNode) { TiXmlText newText(pNewValue); pElement->ReplaceChild(pOldNode, newText); return true; } } return false; } bool XmlDocControl::ModifyElementAttributeValue(TiXmlElement* pElement, const char* pAttributeName, const char* pNewValue) { if (pElement != NULL) { TiXmlAttribute * pOldAttribute = NULL; pOldAttribute = pElement->FirstAttribute(); while (NULL != pOldAttribute) { if (0 == strcmp(pAttributeName, pOldAttribute->Name())) { pOldAttribute->SetValue(pNewValue); } pOldAttribute = pOldAttribute->Next(); } return true; } return false; }