当前位置: 首页 > news >正文

上海做网站建设公司苏州建站网站模板

上海做网站建设公司,苏州建站网站模板,百度网站建设电话销售话术,乐居房产官方网站前面我们介绍了xml文件#xff0c;今天我们试着用boost库来解析xml文件。我们将举两个例子来说明怎么使用。 来自boost官方的例子 先看xml文件的内容#xff1a; debugfilenamedebug.log/filenamemodulesmoduleFinance/modul…前面我们介绍了xml文件今天我们试着用boost库来解析xml文件。我们将举两个例子来说明怎么使用。 来自boost官方的例子 先看xml文件的内容 debugfilenamedebug.log/filenamemodulesmoduleFinance/modulemoduleAdmin/modulemoduleHR/module/moduleslevel2/level /debug 我们再来看如何使用boost读取和保存xml文件。 // ---------------------------------------------------------------------------- // Copyright (C) 2002-2006 Marcin Kalicinski // // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) // // For more information, see www.boost.org // ----------------------------------------------------------------------------#include boost/property_tree/ptree.hpp #include boost/property_tree/xml_parser.hpp #include boost/foreach.hpp #include string #include set #include exception #include iostreamstruct debug_settings {std::string m_file; // log filenameint m_level; // debug levelstd::setstd::string m_modules; // modules where logging is enabledvoid load(const std::string filename);void save(const std::string filename); };void debug_settings::load(const std::string filename) {// Create empty property tree objectusing boost::property_tree::ptree;ptree pt;// Load XML file and put its contents in property tree. // No namespace qualification is needed, because of Koenig // lookup on the second argument. If reading fails, exception// is thrown.read_xml(filename, pt);// Get filename and store it in m_file variable. Note that // we specify a path to the value using notation where keys // are separated with dots (different separator may be used // if keys themselves contain dots). If debug.filename key is // not found, exception is thrown.m_file pt.getstd::string(debug.filename);// Get debug level and store it in m_level variable. This is // another version of get method: if debug.level key is not // found, it will return default value (specified by second // parameter) instead of throwing. Type of the value extracted // is determined by type of second parameter, so we can simply // write get(...) instead of getint(...).m_level pt.get(debug.level, 0);// Iterate over debug.modules section and store all found // modules in m_modules set. get_child() function returns a // reference to child at specified path; if there is no such // child, it throws. Property tree iterator can be used in // the same way as standard container iterator. Category // is bidirectional_iterator.//BOOST_FOREACH(ptree::value_type v, pt.get_child(debug.modules))// m_modules.insert(v.second.data());}void debug_settings::save(const std::string filename) {// Create empty property tree objectusing boost::property_tree::ptree;ptree pt;// Put log filename in property treept.put(debug.filename, m_file);// Put debug level in property treept.put(debug.level, m_level);// Iterate over modules in set and put them in property// tree. Note that the add function places new key at the// end of list of keys. This is fine in most of the// situations. If you want to place item at some other// place (i.e. at front or somewhere in the middle),// this can be achieved using a combination of the insert// and put_value functionsBOOST_FOREACH(const std::string name, m_modules)pt.add(debug.modules.module, name);// Write property tree to XML filewrite_xml(filename, pt); //write_xml(cout,pt); //这个函数有重载. 可以用流 也可直接用文件名. }int main() {try{debug_settings ds;ds.load(debug_settings.xml);ds.save(debug_settings_out.xml);std::cout Success\n;}catch (std::exception e){std::cout Error: e.what() \n;}return 0; } 解析 load函数 首先定义了解析树 using boost::property_tree::ptree;ptree pt; 然后读取xml文件 接下来三行代码读取文件里的内容。 我们注意到 上面的xml的根节点是debug。然后有三个节点filenamemoduleslevel。 其中modules是一个含有子节点的复合节点。 于是 1. m_file pt.getstd::string(debug.filename); 读取filename。如读取失败则抛出异常。 2. m_level pt.get(debug.level, 0); 获取level数当然了我们也可以通过和前一句一样的语法获取m_level m_level pt.getint(debug.level); 但是同样这句话一旦获取不到就会抛出异常如果我们想获取不到返回一个默认值0呢此时可以使用 m_level pt.get(debug.level, 0); 来实现。其中最后返回值的类型通过默认值来推断非常类似c11的auto语法。 3. BOOST_FOREACH(ptree::value_type v, pt.get_child(debug.modules))m_modules.insert(v.second.data()); 由于modules是一个复合节点我们可以通过循环遍历的方法访问节点的子节点。 BOOST_FOREACH类似c11的for(auto value: range) 循环遍历的第一句就是moduleFinance/module而v.firstmodule,v.secondFinance,但是我们要通过data()来获取。 我们可以通过改变上述语句为下面语句验证我的推断 BOOST_FOREACH(ptree::value_type v, pt.get_child(debug.modules)){std::cout v.first v.second.data()std::endl;m_modules.insert(v.second.data());} 值得注意的是我测试的时候发现获取first加不加.data()都可以但获取second必须加.data(). save函数 实际上是read的翻译版只需将get换成put即可.我们只要按照变量对应的标签加即可。 另一个更复杂的例子 xml文件如下 debug namedebugnamefile namedebug.log/modules typeinternalmodule1Finance_Internal/module1module2Admin_Internal/module2module3HR_Internal/module3/modulesmodules typeexternalmoduleFinance_External/modulemoduleAdmin_External/modulemoduleHR_External/module /modules /debug 分析以上xml文件我们会发现此刻带有了属性还有深层嵌套。分析起来稍复杂一些。前面我们讲过xml文件中属性其实可以看成子元素的形式。因此我们对debug遍历的时候第一句应该是namedebugname,第二句是file namedebug.log/ 第三句是 modules typeinternalmodule1Finance_Internal/module1module2Admin_Internal/module2module3HR_Internal/module3/modules第四句是 modules typeexternalmoduleFinance_External/modulemoduleAdmin_External/modulemoduleHR_External/module /modules然后我们看代码 #include iostream #include string #include boost/property_tree/ptree.hpp #include boost/property_tree/xml_parser.hpp #include boost/foreach.hppusing namespace std; using namespace boost::property_tree;int main(void){ptree pt;read_xml(debug_settings2.xml, pt);//loop for every node under debugBOOST_FOREACH(ptree::value_type v1, pt.get_child(debug)){if (v1.first xmlattr){ //its an attribute//read debug namedebugnamecout debug name v1.second.getstring(name) endl;}else if (v1.first file){//read file namedebug.logcout file name v1.second.getstring(xmlattr.name) endl;}else{ // v1.first modules//get module typecout module type: v1.second.getstring(xmlattr.type) endl;//loop for every node under modulesBOOST_FOREACH(ptree::value_type v2, v1.second){if (v2.first xmlattr){ //its an attribute//this can also get module typecout module type again: v2.second.getstring(type) endl;}else{//all the modules have the same structure, so just use data() function.cout module name: v2.second.data() endl;}}//end BOOST_FOREACH}}//end BOOST_FOREACH } 注意 对于属性来说first指”xmlattr“而不是“name”,v.second指的是name的具体值. 参考文献 1.使用Boost property tree来解析带attribute的xml 2.http://www.boost.org/doc/libs/1_46_1/doc/html/boost_propertytree/tutorial.html
http://www.pierceye.com/news/308689/

相关文章:

  • 做外国网站买域名上海网站建设的英文
  • 好看的静态网站信产部网站备案
  • 怎样建设网站 需要哪些条件wordpress安装主题要多久
  • 高端网站设计平台高端网站设计企业印象笔记wordpress同步
  • 汽车网站建设的目的公司简介模板设计图片
  • 做外贸的社交网站怎么攻击网站吗
  • 网站布局手机百度网址大全
  • 企业网站做多大擦边球做网站挣钱
  • 网站怎么备份做网站建设要学多久
  • 怎样做买东西的网站外汇期货喊单网站怎么做的
  • 博客网站推荐郑州哪里做网站
  • 贵州建设职业技术学院网站网站开发 多语言
  • 网站后台管理系统怎么进重庆建设工程安全管理局网站
  • 移动网站开发的视频下载百度网盘下载官网
  • 在百度备案网站建设工程检测网
  • 广州企业网站营销电话公司网站怎么做啊
  • 如何利用视频网站做推广网站开发管理学什么
  • 福建漳发建设有限公司网站做网站申请什么商标
  • 专门做房产的网站上海网站开发毕业生
  • 网站域名已经解析但没有被百度等搜索引擎收录怎么办可以做投票功能的网站
  • 重庆网站设计总部什么是社交电商平台
  • 内容管理网站百度电商平台
  • 网站建设 万网网站统计插件
  • 怎么个人网站设计网站建设 不违背
  • 图片下载网站郑州联通网站备案
  • 名师工作室网站建设 意义o2o新零售系统
  • 域名查询权威网站网页设计基础填空题及答案
  • 网站建设策划方案如何写新开传奇新服网
  • dedecms网站上传服务器不是空间网站正则表达式怎么做
  • 青岛企业建设网站企业南宁网站开发建设