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

本文介绍了一个使用TinyXML库从XML文件中提取数据并递归生成菜单结构的过程。通过解析XML文件,提取特定节点信息,构建菜单层级,并将结果输出到文件中。示例代码展示了如何处理XML节点,生成树形结构并将其写入文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在使用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);
&
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值