您的位置:首页 > 编程语言 > C语言/C++

TinyXml op by C++

2012-03-26 00:00 260 查看
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;
}


$(document).ready(function(){dp.SyntaxHighlighter.HighlightAll('code');});

原文链接:
http://blog.csdn.net/crazyjixiang/article/details/7035010
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: