1. 测试tinyxml库
-
cong@msi:/work/cloud/test$ tree xml/
-
xml/
-
├── a.xml -->测试用的xml
-
├── Makefile -->Makefile
-
├── test.cpp -->测试用demo
-
├── tinystr.cpp -->下面这几个都是从tinyxml中扣出来的
-
├── tinystr.h
-
├── tinyxml.cpp
-
├── tinyxmlerror.cpp
-
├── tinyxml.h
-
└── tinyxmlparser.cpp
-
-
0 directories, 9 files
2. 代码如下
-
cong@msi:/work/cloud/test/xml$ cat test.cpp
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#include <errno.h>
-
#include <iostream>
-
#include <string>
-
-
#include "tinyxml.h"
-
-
#define dbmsg(fmt, args ...) printf("%s:%s[%d]: "fmt"\n", __FILE__,__FUNCTION__, __LINE__,##args)
-
-
using namespace std;
-
-
enum SuccessEnum {FAILURE, SUCCESS};
-
-
SuccessEnum loadXML()
-
{
-
TiXmlDocument doc;
-
//if(!doc.LoadFile("siminfo.xml"))
-
if(!doc.LoadFile("/tmp/siminfo.xml"))
-
{
-
cerr << doc.ErrorDesc() << endl;
-
return FAILURE;
-
}
-
-
TiXmlElement* root = doc.FirstChildElement();
-
if(root == NULL)
-
{
-
cerr << "Failed to load file: No root element." << endl;
-
doc.Clear();
-
return FAILURE;
-
}
-
-
TiXmlElement* e_fid = NULL;
-
TiXmlNode* n_fid= NULL;
-
-
TiXmlElement* e_sw = NULL;
-
TiXmlNode* n_sw= NULL;
-
-
TiXmlElement* e_res_len = NULL;
-
TiXmlNode* n_res_len = NULL;
-
-
TiXmlElement* e_res = NULL;
-
TiXmlNode* n_res = NULL;
-
-
TiXmlElement* e_bin = NULL;
-
-
TiXmlElement* e_item = NULL;
-
TiXmlNode* n_item = NULL;
-
-
for(TiXmlElement* elem = root->FirstChildElement(); elem != NULL; elem = elem->NextSiblingElement())
-
{
-
e_fid = elem->FirstChildElement("FID");
-
n_fid = e_fid->FirstChild();
-
dbmsg("FID=%s",n_fid->ToText()->Value());
-
-
e_sw = elem->FirstChildElement("SW");
-
n_sw = e_sw->FirstChild();
-
dbmsg("SW=%s",n_sw->ToText()->Value());
-
-
e_res_len = elem->FirstChildElement("RES_LENGTH");
-
if(NULL==e_res_len)
-
continue;
-
n_res_len = e_res_len->FirstChild();
-
dbmsg("RES_LENGTH=%s",n_res_len->ToText()->Value());
-
-
e_res = elem->FirstChildElement("RES");
-
if(NULL==e_res)
-
continue;
-
n_res = e_res->FirstChild();
-
dbmsg("RES=%s",n_res->ToText()->Value());
-
-
e_bin = elem->FirstChildElement("BIN");
-
if(NULL==e_bin)
-
continue;
-
e_item = e_bin->FirstChildElement("ITEM");
-
n_item = e_item->FirstChild();
-
dbmsg("ITEM=%s",n_item->ToText()->Value());
-
-
dbmsg();
-
}
-
doc.Clear();
-
return SUCCESS;
-
}
-
-
int main(int argc, char* argv[])
-
{
-
if(loadXML() == FAILURE)
-
return 1;
-
return 0;
-
}
-
cong@msi:/work/cloud/test/xml$
3. 代码下载
xml.rar(下载后改名为xml.tar.gz)
阅读(1045) | 评论(0) | 转发(0) |