使用tinyxml解析XML配置文件

 这是一个项目过程中所遇到的问题。有多个商品种类,每个商品种类有着不同的基本信息,现在我们需要对这些基本信息进行验证。但是每个种类会有自己独特的验证需求(不同种类需要验证的字段可能不一样),如果我们使用代码来判断每个种类需要验证哪些字段会非常麻烦,而且需求稍一变动就要修改源码。所以就想到用配置文件来设置每种商品有哪些字段(信息)需要验证哪些不需要验证。
 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>
 每个<category>标签代表一个品类,里面的标签代表基本信息,其中1表示验证该条信息,0表示不验证该条信息。
 解析XML的方法很多,这里使用的是一个比较简单的方式,使用TinyXML解析器。只需要把TinyXML的源码下载下来解压到你的项目目录中,并引用它们就可以了。
#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;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类
XML入门的常见问题(二)
Java对象的强、软、弱和虚引用
JS解析XML文件和XML字符串详解
java中枚举的详细使用介绍
了解Xml格式
XML入门的常见问题(四)
深入SQLite多线程的使用总结详解
PlayFramework完整实现一个APP(一)
XML和YAML的使用方法
XML轻松学习总节篇