linux开发---29.【C】测试tinyxml库

1040阅读 0评论2017-02-28 wangcong02345
分类:LINUX

1. 测试tinyxml库
  1. cong@msi:/work/cloud/test$ tree xml/
  2. xml/
  3. ├── a.xml                  -->测试用的xml
  4. ├── Makefile               -->Makefile
  5. ├── test.cpp               -->测试用demo
  6. ├── tinystr.cpp            -->下面这几个都是从tinyxml中扣出来的
  7. ├── tinystr.h
  8. ├── tinyxml.cpp
  9. ├── tinyxmlerror.cpp
  10. ├── tinyxml.h
  11. └── tinyxmlparser.cpp

  12. 0 directories, 9 files

2. 代码如下
  1. cong@msi:/work/cloud/test/xml$ cat test.cpp
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <errno.h>
  6. #include <iostream>
  7. #include <string>

  8. #include "tinyxml.h"

  9. #define dbmsg(fmt, args ...) printf("%s:%s[%d]: "fmt"\n", __FILE__,__FUNCTION__, __LINE__,##args)

  10. using namespace std;

  11. enum SuccessEnum {FAILURE, SUCCESS};

  12. SuccessEnum loadXML()
  13. {
  14.     TiXmlDocument doc;
  15.     //if(!doc.LoadFile("siminfo.xml"))
  16.     if(!doc.LoadFile("/tmp/siminfo.xml"))
  17.     {
  18.         cerr << doc.ErrorDesc() << endl;
  19.         return FAILURE;
  20.     }

  21.     TiXmlElement* root = doc.FirstChildElement();
  22.     if(root == NULL)
  23.     {
  24.         cerr << "Failed to load file: No root element." << endl;
  25.         doc.Clear();
  26.         return FAILURE;
  27.     }

  28.     TiXmlElement* e_fid = NULL;
  29.     TiXmlNode* n_fid= NULL;

  30.     TiXmlElement* e_sw = NULL;
  31.     TiXmlNode* n_sw= NULL;

  32.     TiXmlElement* e_res_len = NULL;
  33.     TiXmlNode* n_res_len = NULL;

  34.     TiXmlElement* e_res = NULL;
  35.     TiXmlNode* n_res = NULL;

  36.     TiXmlElement* e_bin = NULL;

  37.     TiXmlElement* e_item = NULL;
  38.     TiXmlNode* n_item = NULL;

  39.     for(TiXmlElement* elem = root->FirstChildElement(); elem != NULL; elem = elem->NextSiblingElement())
  40.     {
  41.         e_fid = elem->FirstChildElement("FID");
  42.         n_fid = e_fid->FirstChild();
  43.         dbmsg("FID=%s",n_fid->ToText()->Value());

  44.         e_sw = elem->FirstChildElement("SW");
  45.         n_sw = e_sw->FirstChild();
  46.         dbmsg("SW=%s",n_sw->ToText()->Value());
  47.     
  48.         e_res_len = elem->FirstChildElement("RES_LENGTH");
  49.         if(NULL==e_res_len)
  50.             continue;
  51.         n_res_len = e_res_len->FirstChild();
  52.         dbmsg("RES_LENGTH=%s",n_res_len->ToText()->Value());

  53.         e_res = elem->FirstChildElement("RES");
  54.         if(NULL==e_res)
  55.             continue;
  56.         n_res = e_res->FirstChild();
  57.         dbmsg("RES=%s",n_res->ToText()->Value());

  58.         e_bin = elem->FirstChildElement("BIN");
  59.         if(NULL==e_bin)
  60.             continue;
  61.         e_item = e_bin->FirstChildElement("ITEM");
  62.         n_item = e_item->FirstChild();
  63.         dbmsg("ITEM=%s",n_item->ToText()->Value());

  64.         dbmsg();
  65.     }
  66.     doc.Clear();
  67.     return SUCCESS;
  68. }

  69. int main(int argc, char* argv[])
  70. {
  71.     if(loadXML() == FAILURE)
  72.         return 1;
  73.     return 0;
  74. }
  75. cong@msi:/work/cloud/test/xml$
3. 代码下载
xml.rar(下载后改名为xml.tar.gz)

上一篇:linux开发---28.【C】用libz库压缩与解压缩的测试
下一篇:linux开发---30.【C】实现内存替换