解析XML格式的配置文件


本人是幼年程序猿,至今尚未成型

在以前的编程中(简直就是闹着玩的)用到的一些外部的参数,要不是在程序执行的时候在命令行中传进去,要不然就是用宏定义。之后才发现人家都是程序读取配置文件,只要程序编译好,需要时只要更改配置文件里的配置选项即可,配置文件一般是xml格式。

tinyxml是一款好用的C++开源的解析工具,在使用前需要将tinystr.cpp,tinyxml.cpp,tinyxmlerror.cpp,tinyxmlparser.cpp,tinyxml.htinystr.h着六个文件拷到你的工程的目录下,需要的话可以在http://sourceforge.net/projects/tinyxml/ 下载。


No Ex You C 个 J8 写个例子吧

Ex1:info.xml    

<?xml version=" 1.0" encoding=" gb2312?>
<School name = "softengine">
         <Class name = "C++">
                 <Student name = "andy" number = "36" phone = "123456789">
                           <email>[email protected]</email>
                           <address>Beijing</address>
                           <sex>man</sex>
                 </Student>
         </Class>
</School>

     Test_XML.cpp

#include<iostream>
#include<string>
#include<map>
#include "tinystr.h"
#include "tinyxml.h"

using namespace std;

void ReadSchoolXml(const char * Conf_File_Name)
{
    TiXmlDocument doc;
    if(!doc.LoadFile(Conf_File_Name)
    {
        cout<<"LoadFile 失败"<<endl;
        return ;
    }
    
    //root节点
    TiXmlElement * rootElement = doc.FirstChild("School");
    cout<<"rootName = "<<rootElement->Value()<<endl;
    
    //class节点
    TiXmlElement * classElement = rootElement->FirstChildElement();
    cout<<"classElement = "<<classElement->Value()<<endl;
   
    //student节点 
    TiXmlElement * studentElement = classElement->FirstChildElement();
    cout<<"studentElment = "<<studentElement->Value()<<endl;
    
    for(;studentElement != NULL; studentElement = studentElement->NextSiblingElement())
    {
        TiXmlAttribute * attributeOfStudent = studentElement->FirstAttribute();
        for(;arrtibuteOfStudent != NULL; attributeOfStudent = attributeOfStudent->Next())
        {
           // cout<<attributeOfStudent->Name()<<" = "<<attributeOfStudent->Value()<<endl;
              string str1 = attributeOfStudent->Name();
              string str2 = attributeOfStudent->Value();
              
              Map[str1] = str2; 
        }
        
        TiXmlElement *studentContactElement = studentElement->FirstChildElement();
        for(;studentContactElement != NULL; studentContactElement = studentContactElement->NextSiblingElement())
        {
            string str1 = studentContactElement->Value();
            string str2 = studentContactElement()->GetText();
            
            Map[str1] = str2;
        }
    }
}

int main(int argc,char ** argv)
{
    if(argc < 1)
    {
        cout<<"缺少参数"<<endl;
        return -1;
    }
    
    map<string,string> Map
    ReadSchoolXml(argv[1],Map);
    
    map<string,string>::iterator it;
    for(it = Map.begin(); it != Map.end(); it++)
    {
        cout<<it->first<<" = "<<it->second<<endl;
    }
    return 0;
}

g++ -o Test_XML tinyxml.cpp tinystr.cpp tinyxmlerror.cpp tinyxmlparser.cpp Test_XML.cpp -g

./Test_XML info.xml 
out:
        address = child
        email = andycgli@tencent
        name = tinyxml
        number = 1234
        phone = 13519141822
        sex = man


Ex 2:

    SERVERCONF.xml

<?xml version=" 1.0" encoding=" gb2312?>
<root>
    <SERVER_IP>10.111.222.333</SERVER_IP>
    <SERVER_PORT>30000</SERVER_PORT>
    <FMODULE_NAME>First_Module</FMODULE_NAME>
    <FMODULE_ID>00001</FMODULE_ID>
    ....
</root>

Tiny_XML.cpp

void doXML(const char * Conf_Name,map<string,string> & Map)
{
    TiXmlDocument doc;
    if(!doc.LoadFile(Conf_Name))
    {
        cout<<"LoadFile 错误<<endl;
        return ;
    }
    
    TiXmlElement * RootElement = doc.RootElement();
    TiXmlElement * AllElement = RootElement->FirstChirdElement();
    
    for(; AllElement != NULL; AllElement = AllElement->NextSiblingElement())
    {
        TiXmlElement * nElement = AllElement->FirstChildElement();
        
        string type = AllElement->Value()
        string value = AllElement->GetText();
        Map[type] = value;
    }
}

int main(int argc,char ** argv)
{
    if(argc != 2)
    {
        cout<<"参数错误"<<endl;
        return -1;
    }
    
    map<string,string> Map;
    
    doXML(argv[1],Map);
    
    map<string,string>::iterator it;
    for(it = Map.begin(); it != Map.end(); it++)
    {
        cout<<it->first<<" = "<<it->second<<endl;
    }
    
    return 0;    
}

编译方法同上
Tiny_XML SRVERCONFIG.xml
out:
     SERVER_IP = 10.111.222.333
     SERVER_PORT = 30000
     FMODULE_NAME = First_Module
     FMODULE_ID = 00001


以此记录我进步的过程

你可能感兴趣的:(C++,xml,tinyxml,解析XML配置文件)