TinyXml op by C++

本文介绍如何使用TinyXML库解析XML配置文件,并提供了一个实际的C++代码示例,展示如何读取并处理XML中的数据。

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

TinyXML is a simple, small, C++ XML parser that can be easily integrated into other programs.

TinyXML SourceCode: http://sourceforge.net/projects/tinyxml/

TinyXML只需要通过first child和next sibling进行创建Dom树。然后通过用根节点的指针为起点进行遍历整棵树。

下面是一段自己操作XML的代码。(C++)

typedef struct QueueInfo* pQueueInfo; struct QueueInfo { int m_iQueueId; char m_chQueueIp[20]; int m_iQueuePort; vector<int> m_vctDataType; }; typedef struct ServerInfo* pServerInfo; struct ServerInfo { string m_mongoIp; int m_mongoPort; string m_mongoDBName; double m_mongoTimeout; string m_logPrefix; string m_logDir; string m_queServerIp; int m_queServerPort; int m_queServerId; int m_queServerCount; int m_queServerDid; vector<QueueInfo> m_vctQueueInfo; }; class ServerConf{ public: bool ReadXmlCfg(const char* _path, pServerInfo _pSI); };


bool ServerConf::ReadXmlCfg(const char* _path, pServerInfo _pSI) { TiXmlDocument* myDocument = new TiXmlDocument(_path); if (NULL == myDocument) { AC_ERROR("Can't init %s", _path); return false; } myDocument->LoadFile(); TiXmlElement* pRoot = myDocument->RootElement(); if (NULL == pRoot) { AC_ERROR("Can't init Xml root"); return false; } //读取MongoServer TiXmlElement* pMongo = pRoot->FirstChildElement("MongoDBServerCfg"); if(NULL == pMongo) { AC_ERROR("Can't find QueueServiceCfg"); return false; } _pSI->m_mongoIp = pMongo->Attribute("serverIp"); _pSI->m_mongoPort = atol(pMongo->Attribute("serverPort")); _pSI->m_mongoDBName = pMongo->Attribute("dbName"); _pSI->m_mongoTimeout = atol(pMongo->Attribute("timeout")); //读取QueueServer TiXmlElement* pQueues = pMongo->NextSiblingElement("QueueServerCfg"); if(NULL == pQueues) { AC_ERROR("Can't find QueueServiceCfg"); return false; } _pSI->m_queServerIp = pQueues->Attribute("serverIp"); _pSI->m_queServerPort = atol(pQueues->Attribute("serverPort")); _pSI->m_queServerId = atol(pQueues->Attribute("id")); _pSI->m_queServerCount = atol(pQueues->Attribute("count")); _pSI->m_queServerDid = atol(pQueues->Attribute("did")); /*循环取出队列配置*/ TiXmlElement* pQueueChild = pQueues->FirstChildElement("queueCfg"); while (NULL != pQueueChild) { struct QueueInfo QIobj; QIobj.m_iQueueId = atoi(pQueueChild->Attribute("id")); /*id*/ memcpy(QIobj.m_chQueueIp, pQueueChild->Attribute("ip"), strlen(pQueueChild->Attribute("ip"))); /*ip*/ QIobj.m_iQueuePort = atoi(pQueueChild->Attribute("port")); /*port*/ char* strBuf = NULL; char* strToken; const char* str = pQueueChild->Attribute("dataType"); /*dataType*/ string strDataType(str); strToken = strtok_r(&strDataType[0],",",&strBuf); while (strToken != NULL) { QIobj.m_vctDataType.push_back(atoi(strToken)); strToken = strtok_r(NULL,",",&strBuf); } _pSI->m_vctQueueInfo.push_back(QIobj); pQueueChild = pQueueChild->NextSiblingElement("queueCfg"); } delete myDocument; return true; }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值