您的位置:首页 > 编程语言 > C语言/C++

C++提取xml文件(使用开源工具类tinyxml)

2017-06-08 17:20 459 查看
最近在使用tinyxml提取xml文件,并递归生成菜单等级的格式
#include "stdafx.h"

#include "tinyxml\tinystr.h"

#include "tinyxml\tinyxml.h"

#include <string>

#include <iostream>

#include <fstream>

#include <algorithm>

#include <vector>

#include <afxwin.h>

#include <map>
using namespace std;
map<string, string> rootMap;
typedef struct tagEleRegister

{

 string strEcuId = "";

 string strCheckCmd = "";

 string strCheckPos = "";

}EleRegister;
typedef struct tagEleCanEcu

{

 string strEcuId = "";

 string strEcuShortName = "";

 string strBusId = "";

 string strReqId = "";

 string strResId = "";

 string strDisplaySeq = "";

}EleCanEcu;
typedef struct tagVariant

{

 string strVarId = "";

 string strVerId = "";

 string strNameStrId = "";

 string strDbPath = "";

 string strDbVarId = "";

 string strProId = "";

 string strProSddId = "";

 string strEcuConfig = "";

 string strFlashType = "";

}Variant;
typedef struct tagEleBean

{

 string beanName = "";

 string filePath = "";

 string modelYear = "";

 string bodyName = "";

 string modelName = "";

}EleBean;
typedef struct tagTree

{

 string strContent;

 vector<tagTree> vctTreeSet;

 tagTree()

 {

  strContent = "";

  vctTreeSet.clear();

 }

}Tree;
typedef struct infoMenu

{

 string str;

 vector<infoMenu> menuList;

 infoMenu()

 {

  str = "";

  menuList.clear();

 }

}tagInfo;
void WriteFileTree(ofstream& outFile, vector<Tree>& vctTreeSet, string strHead)

{

 int iLoop = 0;

 if (0 == vctTreeSet.size())return;

 //outFile.seekp(ios::end);

 for (iLoop = 0; iLoop < vctTreeSet.size(); iLoop++)

 {

  outFile << strHead;

  outFile << vctTreeSet[iLoop].strContent;

  outFile << "\n";

  WriteFileTree(outFile, vctTreeSet[iLoop].vctTreeSet, strHead + "\t");

 }

}
void WriteMenu(ofstream& outFile, vector<tagInfo>& vctTagInfo, string strHead)

{

 int iLoop = 0;

 if (0 == vctTagInfo.size())return;

 //outFile.seekp(ios::end);

 for (iLoop = 0; iLoop < vctTagInfo.size(); iLoop++)

 {

  outFile << strHead;

  if (iLoop == 0 && "" == strHead)

   outFile << vctTagInfo[iLoop].str;

  else if("" == strHead)

   outFile << vctTagInfo[iLoop].str + "\t\t\t\t\t\t\t\\n\\";

  else

   outFile << vctTagInfo[iLoop].str + "\t\t\t\t\t\t\t\\n\\";

  outFile << "\n";

  WriteMenu(outFile, vctTagInfo[iLoop].menuList, strHead + "\t");

 }

}
void AddMenuTree(vector<Tree>& vctTreeSet,vector<string> vctStr,unsigned int uOffset)

{

 unsigned int ulLoop = 0;

 string strTemp;

 if (vctStr.size() <= uOffset)

  return;

 strTemp = vctStr[uOffset];

 for (ulLoop = 0; ulLoop < vctTreeSet.size(); ++ulLoop)

 {

  if (strTemp == vctTreeSet[ulLoop].strContent)break;

 }

 if (ulLoop == vctTreeSet.size())

 {

  vctTreeSet.push_back(Tree());

  vctTreeSet[ulLoop].strContent = strTemp;

 }

 AddMenuTree(vctTreeSet[ulLoop].vctTreeSet, vctStr, 1 + uOffset);

}
void DealYbMaster()

{

 ofstream outFile("Chrysler_Root.txt");

 vector<EleBean> vctEleBean;

 vector<Tree> vctTreeSet;

 string strSrc = "YB_Master.xml";

 //从XML提取内容

 TiXmlDocument tiXmlDoc(strSrc.c_str());

 if (tiXmlDoc.LoadFile())

 {

  TiXmlElement *pRoot = tiXmlDoc.RootElement();

  for (TiXmlNode* pNodeBean = pRoot->FirstChild("bean"); NULL != pNodeBean; pNodeBean = pNodeBean->NextSibling("bean"))

  {

   EleBean eleBean;

   for (TiXmlAttribute* pAttrBean = pNodeBean->ToElement()->FirstAttribute(); NULL != pAttrBean; pAttrBean = pAttrBean = pAttrBean->Next())

   {

    string strTemp;

    strTemp = pAttrBean->Name();

    transform(strTemp.begin(), strTemp.end(), strTemp.begin(), tolower);

    if ("name" == strTemp)

    {

     eleBean.beanName = pAttrBean->Value();

    }

   }

   for (TiXmlNode* pNodeProperty = pNodeBean->FirstChild("property"); NULL != pNodeProperty; pNodeProperty = pNodeProperty->NextSibling("property"))

   {

    TiXmlAttribute* pAttrProperty = pNodeProperty->ToElement()->FirstAttribute();

    string strTemp;

    strTemp = pAttrProperty->Value();

    //transform(strTemp.begin(), strTemp.end(), strTemp.begin(), tolower);

    if ("YBDataFileName" == strTemp)

     eleBean.filePath = pNodeProperty->ToElement()->GetText();

    else if ("modelYear" == strTemp)

     eleBean.modelYear = pNodeProperty->ToElement()->GetText();

    else if ("bodyName" == strTemp)

     eleBean.bodyName = pNodeProperty->ToElement()->GetText();

    else if ("modelName" == strTemp)

     eleBean.modelName = pNodeProperty->ToElement()->GetText();

   }

   vctEleBean.push_back(eleBean);

  }

 }

 //转换path

 for (int i = 0; i < vctEleBean.size(); ++i)

 {

  string::size_type iPos = vctEleBean[i].filePath.rfind('/');

  if (string::npos != iPos)

  {

   string::size_type iCount = vctEleBean[i].filePath.length() - iPos - 8;

   vctEleBean[i].filePath = vctEleBean[i].filePath.substr(iPos + 1, iCount);

  }

 }

 //添加到菜单树

 for (int i = 0; i < vctEleBean.size(); ++i)

 {

  vector<string> vctStr;

  vctStr.push_back(vctEleBean[i].modelYear);

  vctStr.push_back(vctEleBean[i].bodyName + "--" + vctEleBean[i].modelName + "<" + vctEleBean[i].filePath + ">");

  AddMenuTree(vctTreeSet, vctStr, 0);

 }

 //输出到文件

 WriteFileTree(outFile, vctTreeSet, "\t");

}
vector<string> GetDirFileName(string strWildcard)

{

 vector<string> vctStrReturn;

 CFileFind finder;

 CString strFindFile = strWildcard.c_str();

 cout << strFindFile << endl;

 CString strPath = strFindFile.Left(strFindFile.ReverseFind('\\') + 1);

 cout << strPath << endl;

 //BOOL bWorking = finder.FindFile(strFindFile);

 BOOL bWorking = finder.FindFile("*\\*.xml");

 if (bWorking) cout << "File Sucess" << endl;

 while (bWorking)

 {

  bWorking = finder.FindNextFile();

  if (finder.IsDots())

   continue;

  CString strFileName = finder.GetFileName();

  //strFileName = strPath + strFileName;

  string strTemp = strFileName.GetBuffer(0);

  strTemp = "*\\" + strTemp;

  cout << strTemp << endl;

  vctStrReturn.push_back(strTemp);

 }

 return vctStrReturn;

}
vector<tagInfo> DealSingleYB(string& strSrc)

{

 TiXmlDocument* pTiXmlDoc = new TiXmlDocument(strSrc.c_str());

 vector<tagInfo> vctTagInfo;

 if (pTiXmlDoc->LoadFile())

 {

  TiXmlElement* pRoot = pTiXmlDoc->RootElement();

  unsigned int ui = 0;

  for (TiXmlElement* pEleBuiRegisters = pRoot->FirstChildElement(); pEleBuiRegisters != NULL; pEleBuiRegisters = pEleBuiRegisters->NextSiblingElement())

  { 

   string strTemp = pEleBuiRegisters->Value();

   if ("YEAR" == strTemp)

   {

    tagInfo tmpTag;

    tmpTag.str = "0xCD,0x00,0x00,0x00";

    string year = pEleBuiRegisters->GetText();

    string style = pEleBuiRegisters->NextSiblingElement()->GetText();

    map<string, string>::iterator iter = rootMap.find(year+style);

    //cout << iter->second << endl;

    if (iter != rootMap.end())

    {

     tmpTag.str = tmpTag.str + "," + iter->second + "\t\t\"\\";

    }

    else

     tmpTag.str = tmpTag.str + "\t\t\"\\";

    vctTagInfo.push_back(tmpTag);
    tmpTag.str = "[Taskini]";//\t\t\t\t\t\t\t\t\t\\n\\";

    vctTagInfo.push_back(tmpTag);

    ui++;
    tmpTag.str = "Group=";// +"\t\t\t\t\t\t\t\\n\\";

    vctTagInfo[ui].menuList.push_back(tmpTag);
    string str = pEleBuiRegisters->GetText();

    tmpTag.str = "Year=" + str;// +"\t\t\t\t\t\t\t\\n\\";

    vctTagInfo[ui].menuList.push_back(tmpTag);

    //cout << tmpTag.str << endl;
    str = pEleBuiRegisters->NextSiblingElement()->GetText();

    tmpTag.str = "Body=" + str;// +"\t\t\t\t\t\t\t\\n\\";

    vctTagInfo[ui].menuList.push_back(tmpTag);

    //cout << tmpTag.str << endl;

    

   }
   if ("BUILD_REGISTERS" == strTemp)

   {

    for (TiXmlElement* pEleBuiRegister = pEleBuiRegisters->FirstChildElement(); pEleBuiRegister != NULL; pEleBuiRegister = pEleBuiRegister->NextSiblingElement())

    {

     for (TiXmlElement* pEleRegisters = pEleBuiRegister->FirstChildElement(); pEleRegisters != NULL; pEleRegisters = pEleRegisters->NextSiblingElement())

     {

      string strTemp = pEleRegisters->Value();

      if ("REGISTERS" == strTemp)

      {

       for (TiXmlElement* pEleRegister = pEleRegisters->FirstChildElement(); pEleRegister != NULL; pEleRegister = pEleRegister->NextSiblingElement())

       {

        for (TiXmlElement* pEleRegisterContent = pEleRegister->FirstChildElement(); pEleRegisterContent != NULL; pEleRegisterContent = pEleRegisterContent->NextSiblingElement())

        {

         if ("ECU_ID" == pEleRegisterContent->Value())

         {

          string strTemp = pEleRegisterContent->GetText();

         }

        }

       }

      }

     }

    }

   }

   else if ("CAN_ECUS" == strTemp)

   {

    //string ecuNameStr;

    for (TiXmlElement* pEleCanEcu = pEleBuiRegisters->FirstChildElement(); pEleCanEcu != NULL; pEleCanEcu = pEleCanEcu->NextSiblingElement())

    {

     unsigned int num = 0;

     unsigned int Ecu_Name_Num = ui + 1;

     string ecuNameStr;

     for (TiXmlElement* pEleEcuInfo = pEleCanEcu->FirstChildElement(); pEleEcuInfo != NULL; pEleEcuInfo = pEleEcuInfo->NextSiblingElement())

     {

      string strTemp = pEleEcuInfo->Value();

      if ("ECU_LOGICAL_NAME" == strTemp)

      {

       tagInfo tmpTag;

       string ecuNameTemp = pEleEcuInfo->GetText();

       ecuNameStr = ecuNameTemp;
       if(NULL == pEleCanEcu->NextSiblingElement())

        vctTagInfo[1].menuList[0].str = vctTagInfo[1].menuList[0].str + ecuNameStr;

       else

        vctTagInfo[1].menuList[0].str = vctTagInfo[1].menuList[0].str + ecuNameStr + ",";
       tmpTag.str = "[" + ecuNameTemp + "]";

       tmpTag.str = tmpTag.str;// +"\t\t\t\t\t\t\t\t\t\\n\\";

       vctTagInfo.push_back(tmpTag);

       ui++;

       //cout << ecuNameTemp << endl;

      }
      if ("REQUEST_ID" == strTemp)

      {

       tagInfo tmpTag;

       string reqTemp = pEleEcuInfo->GetText();

       tmpTag.str = "Requestid=" + reqTemp;// +"\t\t\t\t\\n\\";

       vctTagInfo[ui].menuList.push_back(tmpTag);

       //cout << reqTemp << endl;

      }
      if ("RESPONSE_ID" == strTemp)

      {

       tagInfo tmpTag;

       string ansTemp = pEleEcuInfo->GetText();

       tmpTag.str = "Responseid=" + ansTemp;// +"\t\t\t\t\\n\\";

       vctTagInfo[ui].menuList.push_back(tmpTag);

       num++;

       //cout << ansTemp << endl;

      }
      if ("VARIANTS" == strTemp)

      {

       int variant_Num = 0;

       tagInfo tmpTag;

       tmpTag.str = "Variant_Number=";

       //tmpTag.str = tmpTag.str +"\t\t\t\t\\n\\";

       vctTagInfo[ui].menuList.push_back(tmpTag);

       num++;

       unsigned int Var_Num = ui;
       for (TiXmlElement* pEleVariants = pEleEcuInfo->FirstChildElement(); pEleVariants != NULL; pEleVariants = pEleVariants->NextSiblingElement())

       {

        string strTemp = pEleVariants->Value();

        if ("VARIANT" == strTemp)

        {

         variant_Num++;

         char num_str[5] = { '\0','\0' ,'\0' ,'\0' ,'\0' };

         sprintf_s(num_str, "%d", variant_Num);

         string str = num_str;

         tagInfo tmpTag;

         tmpTag.str = "Variant";

         tmpTag.str = "[" + ecuNameStr + "_" + tmpTag.str + "_" + str + "]";

         //tmpTag.str = tmpTag.str +"\t\t\t\t\\n\\";

         //vctTagInfo[ui].menuList.push_back(tmpTag);

         //num++;

         vctTagInfo.push_back(tmpTag);

         ui++;

         for (TiXmlElement* pEleVariant = pEleVariants->FirstChildElement(); pEleVariant != NULL; pEleVariant = pEleVariant->NextSiblingElement())

         {

          string strTemp = pEleVariant->Value();

          unsigned int i = 0;

          if ("ECU_VARIANT_ID" == strTemp)

          {

           tagInfo tmpTag;

           string variantTemp = pEleVariant->GetText();

           tmpTag.str = "Variant_Id=" + variantTemp;

           //vctTagInfo[ui].menuList[num].menuList.push_back(tmpTag);

           vctTagInfo[ui].menuList.push_back(tmpTag);

           //cout << variantTemp << endl;

          }
          if ("ECU_VERSION_ID" == strTemp)

          {

           tagInfo tmpTag;

           string versionTemp = pEleVariant->GetText();

           tmpTag.str = "Version_Id=" + versionTemp;

           //vctTagInfo[ui].menuList[num].menuList.push_back(tmpTag);

           vctTagInfo[ui].menuList.push_back(tmpTag);

           //cout << versionTemp << endl;

          }
          if ("DB_PATH" == strTemp)

          {

           tagInfo tmpTag;

           string dbPath = pEleVariant->GetText();

           tmpTag.str = "DB_Path=" + dbPath;

           //vctTagInfo[ui].menuList[num].menuList.push_back(tmpTag);

           vctTagInfo[ui].menuList.push_back(tmpTag);

           //cout << dbPath << endl;

          }
          if ("DB_VARIANT_ID" == strTemp)

          {

           tagInfo tmpTag;

           string dbVar = pEleVariant->GetText();

           tmpTag.str = "DB_Id=" + dbVar;

           //vctTagInfo[ui].menuList[num].menuList.push_back(tmpTag);

           vctTagInfo[ui].menuList.push_back(tmpTag);

           //cout << dbVar << endl;

          }
          if ("PROTOCOL_ID" == strTemp)

          {

           tagInfo tmpTag;

           string protocolId = pEleVariant->GetText();

           tmpTag.str = "Protocol_Id=" + protocolId;

           //vctTagInfo[ui].menuList[num].menuList.push_back(tmpTag);

           vctTagInfo[ui].menuList.push_back(tmpTag);

          }
          if ("PROTOCOL_SDD_ID" == strTemp)

          {

           tagInfo tmpTag;

           string protocol_Sdd = pEleVariant->GetText();

           tmpTag.str = "Protocol_Sdd=" + protocol_Sdd;

           //vctTagInfo[ui].menuList[num].menuList.push_back(tmpTag);

           vctTagInfo[ui].menuList.push_back(tmpTag);

          }

         }

        }

       }

       char num_str[5] = {'\0','\0' ,'\0' ,'\0' ,'\0' };

       sprintf_s(num_str, "%d", variant_Num);

       //itoa(variant_Num, num, 10);

       string str = num_str;

       cout << str << endl;

       vctTagInfo[Var_Num].menuList[num].str = vctTagInfo[Var_Num].menuList[num].str + str;

      }

     }

    }

   }

  }

 }

 delete pTiXmlDoc;

 return vctTagInfo;

}
void DealYbInfo()

{

 ofstream outFile("*.txt");

 vector<string> vctStrFile = GetDirFileName("*.xml");

 vector<vector<tagInfo> > YbInfo;

 for (vector<string>::iterator iter = vctStrFile.begin(); iter != vctStrFile.end(); ++iter)

 {

  vector<tagInfo> tagTemp;

  tagTemp = DealSingleYB(*iter);

  if (tagTemp.size() > 0)

   YbInfo.push_back(tagTemp);

 }
 for (unsigned int uiLoop = 0; uiLoop < YbInfo.size(); uiLoop++)

 {

  WriteMenu(outFile, YbInfo[uiLoop], "");

  outFile << "\t\\n\"" << "\n\n";

 }
}

int main()

{

 cout << "1.处理总表\n2.处理全部文件" << endl;

 

 DealYbInfo();

"pause");

 return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: