解析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 以此记录我进步的过程 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 使用wrap和unwrap加密解密Oracle的PL/SQL对象(包,存储过程
- Sqlite 特殊字符的处理
- c# – 如何修复WCF“net.tcp不支持请求升级”错误?
- postgresql – 在ubuntu服务器上从postgres 9.1升级到9.3
- Vue.js 通过jQuery ajax获取数据实现更新后重新渲染页面的方
- XMLParse 读取XML字段 -- JDOM & DOM4J
- PostgreSql 获取所有的表、视图、字段、 主键
- 字符串里的正则表达
- Webpack+Babel+React开发环境搭建
- c# – 如何在WCF ServiceAuthorizationManager中模拟IConte