使用tinyxml解析XML配置文件
发布时间:2020-12-16 08:59:08 所属栏目:百科 来源:网络整理
导读:这是一个项目过程中所遇到的问题。有多个商品种类,每个商品种类有着不同的基本信息,现在我们需要对这些基本信息进行验证。但是每个种类会有自己独特的验证需求(不同种类需要验证的字段可能不一样),如果我们使用代码来判断每个种类需要验证哪些字段会非常
这是一个项目过程中所遇到的问题。有多个商品种类,每个商品种类有着不同的基本信息,现在我们需要对这些基本信息进行验证。但是每个种类会有自己独特的验证需求(不同种类需要验证的字段可能不一样),如果我们使用代码来判断每个种类需要验证哪些字段会非常麻烦,而且需求稍一变动就要修改源码。所以就想到用配置文件来设置每种商品有哪些字段(信息)需要验证哪些不需要验证。
XML文件的内容如下category.xml所示:
<?xml version="1.0" encoding="utf-8" ?> <categorys> <category> <categoryId>30</categoryId> <brand>1</brand> <colour>1</colour> <marketTime>1</marketTime> <model>1</model> <productFuc>1</productFuc> <netSize>0</netSize> <pkgSize>0</pkgSize> <pkgList>0</pkgList> </category> <category> <categoryId>23</categoryId> <brand>1</brand> <colour>1</colour> <marketTime>1</marketTime> <model>1</model> <productFuc>1</productFuc> <netSize>0</netSize> <pkgSize>0</pkgSize> <pkgList>0</pkgList> </category> </categorys> #include "tinyxml/tinyxml.h" #include "tinyxml/tinystr.h" #include "tinyxml/tinyxml.cpp" #include "tinyxml/tinystr.cpp" #include "tinyxml/tinyxmlparser.cpp" #include "tinyxml/tinyxmlerror.cpp" #include <iostream> #include <string> #include <stdlib.h> #include <map> using namespace std; void getCategoryXmlSet(string& fileName,map<string,int>& name_int_map,int& cgId,int& flag) { flag = 0; //创建一个XML的对象,读取XML文件 TiXmlDocument *myDocument = new TiXmlDocument(fileName.c_str()); myDocument->LoadFile(); //获得根元素 TiXmlElement *rootElement = myDocument->RootElement(); //获得第一个元素 TiXmlElement *FirstElement = rootElement->FirstChildElement(); //依次获得与第一个元素同级的元素 while(FirstElement != NULL) { TiXmlElement *category = FirstElement->FirstChildElement(); string strCategory = category->GetText(); int categoryId = atoi(strCategory.c_str()); if (categoryId == cgId) { flag = 1; //此商品种类有设置 TiXmlElement *Brand = category->NextSiblingElement(); string strBrand = Brand->GetText(); name_int_map["brand"] = atoi(strBrand.c_str()); TiXmlElement *colour = Brand->NextSiblingElement(); string strColour = colour->GetText(); name_int_map["color"] = atoi(strColour.c_str()); TiXmlElement *marketTime = colour->NextSiblingElement(); string strMarketTime = marketTime->GetText(); name_int_map["marketTime"] = atoi(strMarketTime.c_str()); TiXmlElement *model = marketTime->NextSiblingElement(); string strModel = model->GetText(); name_int_map["model"] = atoi(strModel.c_str()); TiXmlElement *productFuc = model->NextSiblingElement(); string strProductFuc = productFuc->GetText(); name_int_map["productFuc"] = atoi(strProductFuc.c_str()); TiXmlElement *netSize = productFuc->NextSiblingElement(); string strNetSize = netSize->GetText(); name_int_map["netSize"] = atoi(strNetSize.c_str()); TiXmlElement *pkgSize = netSize->NextSiblingElement(); string strPkgSize = pkgSize->GetText(); name_int_map["pkgSize"] = atoi(strPkgSize.c_str()); TiXmlElement *pkgList = pkgSize->NextSiblingElement(); string strPkgList = pkgList->GetText(); name_int_map["pkgList"] = atoi(strPkgList.c_str()); break; } //下一个元素 FirstElement = FirstElement->NextSiblingElement(); } } int main() { string fileName = "category.xml"; int cateList[5] = {11,20,23,28,30}; for(int i=0; i<5; ++i) { map<string,int> property_exists; //map用来存放(字段名--数字)字段名称到数字0/1的映射,0--不验证 1--验证 int categoryId = cateList[i]; //商品种类Id(品类) int flag = 0; //flag标志位( 0表示该品类没有在XML中配置,默认验证所有字段) int brand=1,color=1,marketTime=1,model=1,productFuc=1,netSize=1,pkgSize=1,pkgList=1; //字段初始状态位1--验证 getCategoryXmlSet(fileName,property_exists,categoryId,flag); //flag == 1表示该品类在XML中有配置,获取字段验证信息(0--不验证 1--验证) if (flag == 1) { brand = property_exists["brand"]; color = property_exists["color"]; marketTime = property_exists["marketTime"]; model = property_exists["model"]; productFuc = property_exists["productFuc"]; netSize = property_exists["netSize"]; pkgSize = property_exists["pkgSize"]; pkgList = property_exists["pkgList"]; } if (brand) {cout<<"brand need verify"<<endl;} if (color) {cout<<"color need verify"<<endl;} if (marketTime) {cout<<"marketTime need verify"<<endl;} if (model) {cout<<"model need verify"<<endl;} if (productFuc) {cout<<"productFuc need verify"<<endl;} if (netSize) {cout<<"netSize need verify"<<endl;} if (pkgSize) {cout<<"pkgSize need verify"<<endl;} if (pkgList) {cout<<"pkgList need verify"<<endl;} cout<<"====================================="<<endl; } return 0; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |