使用rapidxml读写xml文件
2016-02-02 16:31
507 查看
1、rapidxml 写xml
遍历xml,修改指定属性的值
rapidxml::xml_document<> doc; rapidxml::xml_node<>* rot = doc.allocate_node(rapidxml::node_pi,doc.allocate_string("xml version='1.0' encoding='gb2312'")); doc.append_node(rot); rapidxml::xml_node<>* node = doc.allocate_node(rapidxml::node_element,"config",NULL); doc.append_node(node); for(int i=0;i<5;i++) { rapidxml::xml_node<>* stu = doc.allocate_node(rapidxml::node_element,"student",NULL); node->append_node(stu); char t[256]; sprintf(t, "%d", i); std::string itag=t; std::string strname="test_"+itag; char* pname = doc.allocate_string(strname.c_str()); rapidxml::xml_attribute<>* pAttrType1=doc.allocate_attribute("name",pname); stu->append_attribute(pAttrType1); std::string strage="河北省小山村"+itag; char* page= doc.allocate_string(strage.c_str()); pAttrType1=doc.allocate_attribute("adress",page); stu->append_attribute(pAttrType1); } std::string text; rapidxml::print(std::back_inserter(text), doc, 0); std::ofstream out("config.xml"); out << doc;效果:
遍历xml,修改指定属性的值
setlocale(LC_ALL, ""); // 解决中文路径问题(fstream) rapidxml::file<> f("config.xml"); setlocale(LC_ALL, "C"); rapidxml::xml_document<> doc; //doc.parse<0>(f.data());不包括版本号以及编码 doc.parse<rapidxml::parse_full>(f.data()); rapidxml::xml_node<>* pRoot = doc.first_node(); if(pRoot == NULL) { return; } pRoot = pRoot->next_sibling();//config节点 for(rapidxml::xml_node<>* pExeElem = pRoot->first_node(); pExeElem != NULL; pExeElem = pExeElem->next_sibling()) { std::string szDstType; rapidxml::xml_attribute<>* pAttrType = pExeElem->first_attribute("name"); if(pAttrType != NULL) { szDstType = pAttrType->value(); } if(szDstType.compare("test_1") == 0) { rapidxml::xml_attribute<>* pAttrType1 = pExeElem->first_attribute("adress"); std::string strpath="浙江省"; char* pname = doc.allocate_string(strpath.c_str()); pAttrType1->value(pname); } } std::string text ; rapidxml::print(std::back_inserter(text), doc, 0); setlocale(LC_ALL, ""); // 解决中文路径问题(fstream)= std::ofstream outfile("config2.xml"); setlocale(LC_ALL, "C"); outfile << doc;
相关文章推荐
- 树:并查集
- 跨境电商的趋势分析
- 3.2.3 索引唯一扫描
- 关于数位DP
- SDL入门(一)
- 3.2.2 索引扫描类型
- 闪屏页开发
- iOS-非父子控制器跳转(如:从某一导航控制器的子页面跳转到另一个tabbar控制的页面)
- Android 原生导航 IV-实现Drawer+ToolBar+Tab
- HYSBZ/BZOJ 1004 [HNOI2008] Cards - 组合数学
- 对于机器视觉的应用的总结
- 聊聊并发(1):深入分析Volatile的实现原理
- HDOJ 1133 Buy the tickets!
- 多态的使用和向上向下转型
- 3.1.4 全扫描与高水位线
- 3.1.3 全扫描与多块读取
- MP3文件结构及解码概述
- java.lang.IllegalArgumentException: Page directive: invalid value for import 问题处理
- POJ 2312:Battle City【BFS & 优先队列】
- 3.1.2 全扫描与舍弃