加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

解析XML格式的配置文件

发布时间:2020-12-16 08:40:39 所属栏目:百科 来源:网络整理
导读:本人是幼年程序猿,至今尚未成型 在以前的编程中(简直就是闹着玩的)用到的一些外部的参数,要不是在程序执行的时候在命令行中传进去,要不然就是用宏定义。之后才发现人家都是程序读取配置文件,只要程序编译好,需要时只要更改配置文件里的配置选项即可,


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


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

<?xmlversion="1.0"encoding="gb2312?>
<Schoolname="softengine">
<Classname="C++">
<Studentname="andy"number="36"phone="123456789">
<email>andy@163.com</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"

usingnamespacestd;

voidReadSchoolXml(constchar*Conf_File_Name)
{
TiXmlDocumentdoc;
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;
stringstr1=attributeOfStudent->Name();
stringstr2=attributeOfStudent->Value();

Map[str1]=str2;
}

TiXmlElement*studentContactElement=studentElement->FirstChildElement();
for(;studentContactElement!=NULL;studentContactElement=studentContactElement->NextSiblingElement())
{
stringstr1=studentContactElement->Value();
stringstr2=studentContactElement()->GetText();

Map[str1]=str2;
}
}
}

intmain(intargc,char**argv)
{
if(argc<1)
{
cout<<"缺少参数"<<endl;
return-1;
}

map<string,string>Map
ReadSchoolXml(argv[1],Map);

map<string,string>::iteratorit;
for(it=Map.begin();it!=Map.end();it++)
{
cout<<it->first<<"="<<it->second<<endl;
}
return0;
}

g++-oTest_XMLtinyxml.cpptinystr.cpptinyxmlerror.cpptinyxmlparser.cppTest_XML.cpp-g

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


Ex 2:

SERVERCONF.xml

<?xmlversion="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

voiddoXML(constchar*Conf_Name,map<string,string>&Map)
{
TiXmlDocumentdoc;
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();

stringtype=AllElement->Value()
stringvalue=AllElement->GetText();
Map[type]=value;
}
}

intmain(intargc,char**argv)
{
if(argc!=2)
{
cout<<"参数错误"<<endl;
return-1;
}

map<string,string>Map;

doXML(argv[1],string>::iteratorit;
for(it=Map.begin();it!=Map.end();it++)
{
cout<<it->first<<"="<<it->second<<endl;
}

return0;
}

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


以此记录我进步的过程

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读