1.//包含XML解析类文件头 #include "tinyxml.h" 2.定义变量 TiXmlDocument * m_pXml; bool m_bLoadXml; 3.读函数 BOOL CAlarmDlg::ReadCFGFile() { m_pXml=new TiXmlDocument(); BOOL flag=FALSE; #ifdef MIPS m_bLoadXml=m_pXml->LoadFile("//Hive//configfiles//Alarm//Alarm.xml"); CString sz_xmlfilename=L"//Hive//configfiles//Alarm//Alarm.xml"; DWORD result=GetFileAttributes(sz_xmlfilename); SetFileAttributes(sz_xmlfilename,result|FILE_ATTRIBUTE_READONLY); #else m_bLoadXml=m_pXml->LoadFile("//Storage Card//configfiles//Alarm//Alarm.xml"); CString sz_xmlfilename=L"//Storage Card//configfiles//Alarm//Alarm.xml"; DWORD result=GetFileAttributes(sz_xmlfilename); SetFileAttributes(sz_xmlfilename,result|FILE_ATTRIBUTE_READONLY); #endif CString temp1,temp2,temp3; if(m_bLoadXml) { TiXmlElement* rootElement = m_pXml->RootElement(); //<Alarm> TiXmlElement* alarmElement = rootElement->FirstChildElement(); //<ALARM> alarmElement=alarmElement->FirstChildElement();//子一级<ALARM> //获取服务器地址 while(alarmElement) { //继续轮询下一个<ALARM> m_szaAlarmTime.Add(alarmElement->GetText()); alarmElement=alarmElement->NextSiblingElement(); } flag=TRUE; } //回收内存 if(m_pXml!=NULL) { delete m_pXml; m_pXml=NULL; } return flag; } 4.写函数 BOOL CSetTimeDlg::WriteCFGFile() { m_pXml=new TiXmlDocument(); BOOL flag=FALSE; #ifdef MIPS m_bLoadXml=m_pXml->LoadFile("//Hive//configfiles//Alarm//Alarm.xml"); CString sz_xmlfilename=L"//Hive//configfiles//Alarm//Alarm.xml"; DWORD result=GetFileAttributes(sz_xmlfilename); result&=~FILE_ATTRIBUTE_READONLY; SetFileAttributes(sz_xmlfilename,result); #else m_bLoadXml=m_pXml->LoadFile("//Storage Card//configfiles//Alarm//Alarm.xml"); CString sz_xmlfilename=L"//Storage Card//configfiles//Alarm//Alarm.xml"; DWORD result=GetFileAttributes(sz_xmlfilename); result&=~FILE_ATTRIBUTE_READONLY; SetFileAttributes(sz_xmlfilename,result); #endif CString temp1,temp2,temp3; int count; if(m_bLoadXml) { TiXmlElement* rootElement = m_pXml->RootElement(); //<MAIN> TiXmlElement* alarmElement = rootElement->FirstChildElement(); //<ALARM> alarmElement=alarmElement->FirstChildElement();//子一级<ALARM> count=0; //获取服务器地址 if(m_balarmcountisadd==FALSE) { while(alarmElement) { //继续轮询下一个<ALARM> if(count<m_ialrmtimeindex) { count+=1; alarmElement=alarmElement->NextSiblingElement(); } else { int m_count=0; alarmElement->Clear(); CString temp; //小时 m_count=_wtoi(m_szhour); if(m_count<10) m_szhour.Format(L"0%d",m_count); else m_szhour.Format(L"%d",m_count); temp=m_szhour; //冒号 temp+=L":"; //分钟 m_count=_wtoi(m_szminute); if(m_count<10) m_szminute.Format(L"0%d",m_count); else m_szminute.Format(L"%d",m_count); temp+=m_szminute; //Unicode转为ASCIIm码 DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,temp.GetBuffer(temp.GetLength()),-1,NULL,0,NULL,FALSE); temp.ReleaseBuffer(); char *psText; psText = new char[dwNum]; WideCharToMultiByte (CP_OEMCP,NULL,temp.GetBuffer(temp.GetLength()),-1,psText,dwNum,NULL,FALSE); temp.ReleaseBuffer(); TiXmlText *pValue = new TiXmlText(psText); alarmElement->LinkEndChild(pValue); delete []psText; #ifdef MIPS m_pXml->SaveFile("//Hive//configfiles//Alarm//Alarm.xml"); CString sz_xmlfilename=L"//Hive//configfiles//Alarm//Alarm.xml"; DWORD result=GetFileAttributes(sz_xmlfilename); SetFileAttributes(sz_xmlfilename,result|FILE_ATTRIBUTE_READONLY); #else m_pXml->SaveFile("//Storage Card//configfiles//Alarm//Alarm.xml"); CString sz_xmlfilename=L"//Storage Card//configfiles//Alarm//Alarm.xml"; DWORD result=GetFileAttributes(sz_xmlfilename); SetFileAttributes(sz_xmlfilename,result|FILE_ATTRIBUTE_READONLY); #endif flag=TRUE; break; } } } else { //while(alarmElement) TiXmlElement* rootElement = m_pXml->RootElement(); //<MAIN> TiXmlElement* alarmElement = rootElement->FirstChildElement(); //<ALARM> int m_count=0; CString temp; //小时 m_count=_wtoi(m_szhour); if(m_count<10) m_szhour.Format(L"0%d",m_count); else m_szhour.Format(L"%d",m_count); temp=m_szhour; //冒号 temp+=L":"; //分钟 m_count=_wtoi(m_szminute); if(m_count<10) m_szminute.Format(L"0%d",m_count); else m_szminute.Format(L"%d",m_count); temp+=m_szminute; //Unicode转为ASCIIm码 DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,temp.GetBuffer(temp.GetLength()),-1,NULL,0,NULL,FALSE); temp.ReleaseBuffer(); char *psText; psText = new char[dwNum]; WideCharToMultiByte (CP_OEMCP,NULL,temp.GetBuffer(temp.GetLength()),-1,psText,dwNum,NULL,FALSE); temp.ReleaseBuffer(); TiXmlElement* alarm = new TiXmlElement("ALARM"); TiXmlText *pValue = new TiXmlText(psText); alarm->LinkEndChild(pValue); alarmElement->LinkEndChild(alarm); delete []psText; #ifdef MIPS m_pXml->SaveFile("//Hive//configfiles//Alarm//Alarm.xml"); CString sz_xmlfilename=L"//Hive//configfiles//Alarm//Alarm.xml"; DWORD result=GetFileAttributes(sz_xmlfilename); SetFileAttributes(sz_xmlfilename,result|FILE_ATTRIBUTE_READONLY); #else m_pXml->SaveFile("//Storage Card//configfiles//Alarm//Alarm.xml"); CString sz_xmlfilename=L"//Storage Card//configfiles//Alarm//Alarm.xml"; DWORD result=GetFileAttributes(sz_xmlfilename); SetFileAttributes(sz_xmlfilename,result|FILE_ATTRIBUTE_READONLY); #endif flag=TRUE; } } //回收内存 if(m_pXml!=NULL) { delete m_pXml; m_pXml=NULL; } return flag; } 5.xml文件类似于 <ALARM> <ALARM> <ALARM>14:36</ALARM> <ALARM>09:06</ALARM> <ALARM>22:00</ALARM> <ALARM>05:00</ALARM> <ALARM>09:00</ALARM> </ALARM> </ALARM> |