zoukankan      html  css  js  c++  java
  • C++:借助tinyxml2读取XML文件

    // XMLT01.cpp : 定义控制台应用程序的入口点。
    //
    #include "stdafx.h"
    #include <iostream>
    #include "tinyxml2.h"

    using namespace std;
    using namespace TinyXml2;

    void ReadTest01XML()
    {
     XMLDocument doc;
     doc.LoadFile("Test01.xml");
     const char * content = doc.FirstChildElement("test")->GetText();
     printf("%s ",content);
    }

    void Printfln(const char * content, const char * name = "",const int n = 0, const char * notEqual1 = "", const char * notEqual2 = NULL)
    {
     for(int i = 0; i < n; i++)
     {
      printf("    ");
     }
     if(content != notEqual1 && content != notEqual2)
     {
      printf("%s: %s ",name,content);
     }
     else
     {
      printf("%s: ",name);
     }
    }

    void ReadXML(const XMLElement *root)
    {
     if(NULL == root)
     {
      return;
     }

     static int flag = 0;

     const char * rootName = NULL;
     const char * rootContent = NULL;
     const XMLAttribute  * rootAttribute = NULL;
     const char * rootAttributeName = NULL;
     const char * rootAttributeValue = NULL;
     rootName = root->Name();
     rootContent = root->GetText();
     rootAttribute = root->FirstAttribute();
     if(NULL != rootAttribute)
     {
      rootAttributeName = rootAttribute->Name();
      rootAttributeValue = rootAttribute->Value();
     }
     Printfln(rootContent,rootName,flag);
     Printfln(rootAttributeValue,rootAttributeName,flag);

     const XMLElement *child = root->FirstChildElement();
     if(NULL != child)
     {
      flag++;
      ReadXML(child);
     }

     const XMLElement * nextSibling = root->NextSiblingElement();
     if(NULL != nextSibling)
     {
      ReadXML(nextSibling);
     }
     else
     {
      flag--;
      return;
     }
    }

    void ReadTest02XML()
    {
     XMLDocument doc;
     doc.LoadFile("Test02.xml");
     const XMLElement *root = doc.RootElement();

     ReadXML(root);

     system("pause");
    }


    int _tmain(int argc, _TCHAR* argv[])
    {
     ReadTest01XML();
     ReadTest02XML();
     return 0;
    }

  • 相关阅读:
    PowerShell2.0之Windows排错(六)检查网络故障
    确保数据安全是云计算取信于用户的关键
    企业发展如何借助“云的力量”
    PowerShell2.0之维护网络(三)设置网络适配器
    Feign最佳实践
    Nacos注册中心原理
    GateWay网关快速入门
    Nacos集群搭建
    Feign快速入门
    Feign的性能优化
  • 原文地址:https://www.cnblogs.com/shenchao/p/3140619.html
Copyright © 2011-2022 走看看