zoukankan      html  css  js  c++  java
  • CMarkup 解析XML

      1 #include "StdAfx.h"
      2 #include "CIMFile.h"
      3 #include "Markup.h"
      4 
      5 #ifdef  UNICODE 
      6     #define _tcout(x) std::wcout << x;
      7 #else
      8     #define _tcout(x) std::cout << x;
      9 #endif
     10 
     11 char* Wide2Byte(const TCHAR* pWide)
     12 {
     13     char *pBytes = NULL;
     14     int nLen = WideCharToMultiByte(CP_ACP,0,pWide,-1,NULL,0,NULL,NULL);
     15     // get need convert length
     16     int i = (int)wcslen(pWide)*sizeof(TCHAR);
     17     // get need new memory size
     18     pBytes = new char[i+1];
     19     if (!pBytes) // if fail then new again
     20     {
     21             pBytes = new char[i+1];
     22     }
     23     WideCharToMultiByte(CP_ACP,0,pWide,-1,pBytes,nLen,NULL,NULL);
     24     pBytes[nLen]='\0';
     25 
     26     return pBytes;
     27 }
     28 
     29 TCHAR* Byte2Wide(const char* pBytes)
     30 {
     31     TCHAR *pWide = NULL;
     32 
     33     int nLen = MultiByteToWideChar(CP_ACP,0,pBytes,strlen(pBytes),NULL,0);
     34     pWide = new wchar_t[nLen+1];    
     35     if (!pWide) // if fail then new again
     36     {
     37         pWide = new TCHAR[nLen+1];
     38     }
     39     MultiByteToWideChar(CP_ACP,0,pBytes,strlen(pBytes),pWide,nLen); 
     40     pWide[nLen]='\0';
     41 
     42     return pWide;
     43 }
     44 
     45 void _FindDirFiles(tstring dirPath, tstring suffix, vector<tstring> &files)
     46 {
     47     TCHAR *_dirPath = new TCHAR[MAX_PATH];
     48     int len = dirPath.length() + 4;
     49     _tcscpy_s(_dirPath, len, dirPath.c_str());
     50     _tcscat_s(_dirPath, len, _T("*.*"));
     51 
     52     _finddata_t file;
     53 
     54     char *pdirPath = Wide2Byte(_dirPath);
     55 
     56     long lf;
     57     if((lf = _findfirst(pdirPath, &file))==-1l)
     58         cout<<"文件没有找到!\n";
     59     else
     60     {
     61         files.clear();
     62         while( _findnext( lf, &file ) == 0 )
     63         {
     64             TCHAR *pName = Byte2Wide(file.name);
     65             tstring str(pName);
     66             int te = str.find(suffix);
     67             if(te != -1){
     68                 //cout<<file.name<<endl;
     69                 files.push_back(str);
     70             }
     71             if(file.attrib == _A_NORMAL)cout<<"  普通文件  ";
     72             else if(file.attrib == _A_RDONLY)cout<<"  只读文件  ";
     73             else if(file.attrib == _A_HIDDEN )cout<<"  隐藏文件  ";
     74             else if(file.attrib == _A_SYSTEM )cout<<"  系统文件  ";
     75             else if(file.attrib == _A_SUBDIR){
     76 
     77                 if(strcmp(file.name,"..")==0 || strcmp(file.name,".svn")==0){}
     78             }
     79             
     80             delete[] pName;
     81         }
     82     }
     83     delete[] pdirPath;
     84     delete[] _dirPath;
     85 
     86     _findclose(lf);
     87 
     88 }
     89 
     90 void FindDirFiles(string dirPath, string suffix, vector<string> &files)
     91 {
     92     char *pchar = new char[MAX_PATH];
     93     int len = dirPath.length() + 4;
     94     strcpy_s(pchar, len, dirPath.c_str());
     95     strcat_s(pchar, len, "*.*");
     96 
     97     _finddata_t file;
     98 
     99     long lf;
    100     if((lf = _findfirst(pchar, &file))==-1l)
    101         cout<<"文件没有找到!\n";
    102     else
    103     {
    104         files.clear();
    105         while( _findnext( lf, &file ) == 0 )
    106         {
    107 
    108             string str(file.name);
    109             int te = str.find(suffix.c_str());
    110             if(te != -1){
    111                 //cout<<file.name<<endl;
    112                 files.push_back(str);
    113             }
    114             if(file.attrib == _A_NORMAL)cout<<"  普通文件  ";
    115             else if(file.attrib == _A_RDONLY)cout<<"  只读文件  ";
    116             else if(file.attrib == _A_HIDDEN )cout<<"  隐藏文件  ";
    117             else if(file.attrib == _A_SYSTEM )cout<<"  系统文件  ";
    118             else if(file.attrib == _A_SUBDIR){
    119 
    120                 if(strcmp(file.name,"..")==0 || strcmp(file.name,".svn")==0){}
    121             }
    122         }
    123     }
    124     delete[] pchar;
    125 
    126     _findclose(lf);
    127 
    128 }
    129 
    130 CCIMFile::CCIMFile(void) :
    131     m_arSubstation(10000)
    132     , m_arPDLoadSWDet(10000)
    133 {
    134     m_pXML = new CMarkup();
    135 }
    136 
    137 CCIMFile::~CCIMFile(void)
    138 {
    139     CleanMem();
    140     DELETE_POINTER(m_pXML);
    141 }
    142 
    143 void CCIMFile::CleanMem()
    144 {
    145     m_arSubstation.ReleaseAll();
    146     m_arSWGear.ReleaseAll();
    147     m_arPDRoom.ReleaseAll();
    148     m_arPDSite.ReleaseAll();
    149     m_arPDHVLine.ReleaseAll();
    150     m_arPDLeadLineDet.ReleaseAll();
    151     m_arPDMainBusDet.ReleaseAll();
    152     m_arPDBreaker.ReleaseAll();
    153     m_arPDSwitch.ReleaseAll();
    154     m_arPDSubSWDet.ReleaseAll();
    155     m_arPDLoadSW.ReleaseAll();
    156     m_arPDLoadSWDet.ReleaseAll();
    157     m_arPDFusePR.ReleaseAll();
    158     m_arPDFuse.ReleaseAll();
    159     m_arPDTran.ReleaseAll();
    160     m_arPDXFmrDet.ReleaseAll();
    161 
    162     //DeleteMap(&mpTbl);
    163     mpTbl.clear();
    164 
    165 }
    166 
    167 BOOL CCIMFile::ParseDir(LPCTSTR strDir)
    168 {
    169     CleanMem();
    170 
    171     tstring suffix = _T(".xml");
    172     vector<tstring> files;
    173     _FindDirFiles(strDir, suffix, files);
    174     int num = files.size();
    175     cout << "    文件总数:" << num << endl << endl;
    176     
    177     for (int i = 0; i < (int)files.size(); i++)
    178     {
    179         tstring filePath = strDir + files[i];
    180         int nLen = files[i].length();
    181         TCHAR *strTemp = new TCHAR[nLen + 1];
    182         _tcscpy_s(strTemp, nLen + 1, files[i].c_str());
    183         wcstok(strTemp, _T("_"));
    184         CIM_NODE::nParseTblId = _ttoi(wcstok(NULL, _T("_")));
    185         DELETE_POINTER(strTemp);
    186         
    187         wcout << "  ["<< i << "/" << num << "] 正在解析:" << files[i].c_str();
    188         ParseFile(filePath.c_str());
    189     }
    190 
    191     wcout << "    解析完毕!" << endl;
    192 
    193     return TRUE;
    194 }
    195 
    196 BOOL CCIMFile::ParseFile(LPCTSTR strFilePath)
    197 {
    198     //string path = "D:\\iStudio\\svn\\SZ_REVA\\PowerES\\prog\\data\\GISCIM\\";
    199 
    200     int numsOfPSR = 0;
    201 
    202     if(!m_pXML->Load((MCD_CSTR)strFilePath))
    203         return FALSE;
    204 
    205     switch (CIM_NODE::nParseTblId)
    206     {
    207     case 76:    // CIM_DIST_LOADSWITCHDET
    208         {
    209             m_pXML->ResetMainPos();
    210             CIM_NODE::strParseTblName = CIM_DIST_LOADSWITCHDET::strTblName;
    211             while (m_pXML->FindChildElem(MCD_CSTR(_T("cim:") + CIM_DIST_LOADSWITCHDET::strTblName)))
    212             {
    213                 CIM_DIST_LOADSWITCHDET *pNode = m_arPDLoadSWDet.AddItem();
    214                 pNode->Parse(m_pXML);
    215             
    216                 mpNode.insert(pair<int, CIM_NODE *>(pNode->nID, pNode));
    217                 mpTbl.insert(pair<int, CIM_NODE *>(CIM_NODE::nParseTblId, pNode));
    218 
    219                 numsOfPSR++;
    220             }
    221 
    222         }
    223         break;
    224     }
    225 
    226     wcout << "\t 新增解析对象个数:" << numsOfPSR << "  ,共 " << mpNode.size() << "" << endl << endl;
    227 
    228     return TRUE;
    229 }
  • 相关阅读:
    单例模式
    Curator Zookeeper分布式锁
    LruCache算法原理及实现
    lombok 简化java代码注解
    Oracle客户端工具出现“Cannot access NLS data files or invalid environment specified”错误的解决办法
    解决mysql Table ‘xxx’ is marked as crashed and should be repaired的问题。
    Redis 3.0 Cluster集群配置
    分布式锁的三种实现方式
    maven发布项目到私服-snapshot快照库和release发布库的区别和作用及maven常用命令
    How to Use Convolutional Neural Networks for Time Series Classification
  • 原文地址:https://www.cnblogs.com/benevo/p/3106094.html
Copyright © 2011-2022 走看看