Linux下使用libxml2解析XML配置文件

这是一个项目过程中所遇到的问题。有多个商品种类,每个商品种类有着不同的基本信息,现在我们需要对这些基本信息进行验证。但是每个种类会有自己独特的验证需求(不同种类需要验证的字段可能不一样),如果我们使用代码来判断每个种类需要验证哪些字段会非常麻烦,而且需求稍一变动就要修改源码。所以就想到用配置文件来设置每种商品有哪些字段(信息)需要验证哪些不需要验证。

与上一篇文章使用tinyxml解析XML配置文件有着相同的功能,这是对解析XML文件另一种方法的尝试。两种方式在保存解析出的配置信息的方法上有所不同,tinyxml使用map来保存配置数据,这里是使用数组来保存配置数据。

先贴出要解析的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 <iostream>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <string>
#include <stdlib.h>

using namespace std;

void readXMLBetter(string& fileName, int (&arrays)[8], int arraySize, int& cgId, int& flag)
{
    xmlDocPtr doc = xmlParseFile(fileName.c_str());
    if (doc == NULL)
    {
        cout<<"Read xml failed."<<endl;
        return;
    }
    
    xmlNodePtr curElement = xmlDocGetRootElement(doc);
    if (curElement == NULL)
    {
        cout<<"This xml file is empty."<<endl;
        return;
    }
    
    curElement = curElement->xmlChildrenNode;
    while (curElement)
    {
        //如果是text则链表向下走一位
        if (!xmlStrcmp(curElement->name,(const xmlChar*)"text"))
        {
            curElement = curElement->next;
            if (curElement == NULL)
                break;
        }
        
        xmlNodePtr curNode = curElement->xmlChildrenNode;

        //得到categoryId,判断是否跟cgId相等
        if (!xmlStrcmp(curNode->name,(const xmlChar*)"text"))
        {
            curNode = curNode->next;
            if (curNode == NULL)
                break;
        }
        xmlChar* tmp = xmlNodeGetContent(curNode);
        int categoryId = atoi((char *)tmp);
        xmlFree(tmp);
        curNode = curNode->next;
        
        if (categoryId == cgId)
        {
            int i = 0;
            while(curNode)
            {
                if (!xmlStrcmp(curNode->name,(const xmlChar*)"text"))
                {
                    curNode = curNode->next;
                    if (curNode == NULL)
                        break;
                }
                xmlChar* tmp = xmlNodeGetContent(curNode);
                if (i < arraySize)
                    arrays[i++] = atoi((char *)tmp);
                
                xmlFree(tmp);                
                curNode = curNode->next;
            }                                   
        }
        
        curElement = curElement->next;
    }
    
    xmlFree(doc);
}

int main()
{
    string fileName = "category.xml";
    int categorys[5] = {12, 23, 25, 30, 45};
    
    
    for (int i = 0; i < 5; ++i)
    {
        int flag = 0;
        int arrays[8] = {1, 1, 1, 1, 1, 1, 1, 1};
        readXMLBetter(fileName, arrays, 8, categorys[i], flag);
        
        int brand = arrays[0];
        int color = arrays[1];
        int marketTime = arrays[2];
        int model = arrays[3];
        int productFuc = arrays[4];
        int netSize = arrays[5];
        int pkgSize = arrays[6];
        int pkgList = arrays[7];
        
        cout<<"categoryId ============ "<<categorys[i]<<endl;
        cout<<"brand:"<<brand<<endl;
        cout<<"color:"<<color<<endl;
        cout<<"marketTime:"<<marketTime<<endl;
        cout<<"model:"<<model<<endl;
        cout<<"productFuc:"<<productFuc<<endl;
        cout<<"netSize:"<<netSize<<endl;
        cout<<"pkgSize:"<<pkgSize<<endl;
        cout<<"pkgList:"<<pkgList<<endl;    
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值