使用rapidxml操作xml~读写文件操作(转)
2012-09-22 16:30
337 查看
rapidxml~网上很容易下,下面介绍使用方法:
1、xml写文件
如下:
生成的xml文件为:
2、读xml文件
需要说明的是rapidxml明显有一个bug
那就是append_node(doc.allocate_node(node_element,"h","0"));的时候并不考虑该对象是否存在!
转自:http://songhao0201.blog.163.com/blog/static/168027617201131323013399/
ps:对于常量节点名称,值或属性,直接doc.allocate_node(node_element,"color",NULL);没关系,但是有时程序是变量节点,这里要先申请内存:
char* pname = doc.allocate_string(s.c_str());
doc.allocate_node(node_element,pname,NULL)
参考:http://blog.csdn.net/zhaoze87/article/details/7205061
http://www.cppblog.com/tx7do/archive/2010/08/18/123779.html
1、xml写文件
如下:
#include <iostream> #include <rapidxml/rapidxml.hpp> #include <rapidxml/rapidxml_utils.hpp> #include <rapidxml/rapidxml_print.hpp> using namespace rapidxml; int main() { xml_document<> doc; xml_node<>* rot = doc.allocate_node(rapidxml::node_pi,doc.allocate_string("xml version='1.0' encoding='utf-8'")); doc.append_node(rot); xml_node<>* node = doc.allocate_node(node_element,"config","information"); xml_node<>* color = doc.allocate_node(node_element,"color",NULL); //建议使用如下方法,否则临时变量的指针出了作用域,容易造成xml节点问题 //std::string s = "color"; //char* pname = doc.allocate_string(s.c_str()); //doc.allocate_node(node_element,pname,NULL) doc.append_node(node); node->append_node(color); color->append_node(doc.allocate_node(node_element,"red","0.1")); color->append_node(doc.allocate_node(node_element,"green","0.1")); color->append_node(doc.allocate_node(node_element,"blue","0.1")); color->append_node(doc.allocate_node(node_element,"alpha","1.0")); xml_node<>* size = doc.allocate_node(node_element,"size",NULL); size->append_node(doc.allocate_node(node_element,"x","640")); size->append_node(doc.allocate_node(node_element,"y","480")); node->append_node(size); xml_node<>* mode = doc.allocate_node(rapidxml::node_element,"mode","screen mode"); mode->append_attribute(doc.allocate_attribute("fullscreen","false")); node->append_node(mode); std::string text; rapidxml::print(std::back_inserter(text), doc, 0); std::cout<<text<<std::endl; std::ofstream out("config.xml"); out << doc; system("PAUSE"); return EXIT_SUCCESS; }
生成的xml文件为:
<?xml version='1.0' encoding='utf-8' ?> <config> <color> <red>0.1</red> <green>0.1</green> <blue>0.1</blue> <alpha>1.0</alpha> </color> <size> <x>640</x> <y>480</y> </size> <mode fullscreen="false">screen mode</mode> </config>
2、读xml文件
#include <iostream> #include <rapidxml/rapidxml.hpp> #include <rapidxml/rapidxml_utils.hpp> #include <rapidxml/rapidxml_print.hpp> using namespace rapidxml; int main() { file<> fdoc("config.xml"); std::cout<<fdoc.data()<<std::endl; xml_document<> doc; doc.parse<parse_full>(fdoc.data()); std::cout<<doc.name()<<std::endl; //! 获取根节点 xml_node<>* root = doc.first_node("config"); std::cout<<root->name()<<std::endl; //! 获取根节点第一个节点 xml_node<>* node1 = root->first_node(); std::cout<<node1->name()<<std::endl; xml_node<>* node11 = node1->first_node(); std::cout<<node11->name()<<std::endl; std::cout<<node11->value()<<std::endl; //! 修改之后再次保存 xml_node<>* size = root->first_node("size"); size->append_node(doc.allocate_node(node_element,"w","0")); size->append_node(doc.allocate_node(node_element,"h","0")); std::string text; rapidxml::print(std::back_inserter(text),doc,0); std::cout<<text<<std::endl; std::ofstream out("config.xml"); out << doc; system("PAUSE"); return EXIT_SUCCESS; }
需要说明的是rapidxml明显有一个bug
那就是append_node(doc.allocate_node(node_element,"h","0"));的时候并不考虑该对象是否存在!
转自:http://songhao0201.blog.163.com/blog/static/168027617201131323013399/
ps:对于常量节点名称,值或属性,直接doc.allocate_node(node_element,"color",NULL);没关系,但是有时程序是变量节点,这里要先申请内存:
char* pname = doc.allocate_string(s.c_str());
doc.allocate_node(node_element,pname,NULL)
参考:http://blog.csdn.net/zhaoze87/article/details/7205061
http://www.cppblog.com/tx7do/archive/2010/08/18/123779.html
相关文章推荐
- 使用rapidxml操作xml~读写文件操作
- 使用rapidxml操作xml~读写文件操作
- 使用rapidxml操作xml~读写文件操作
- c# winform app.config xml 配置文件 读写操作
- 读写文件操作xml
- Spring中c3p0连接池的配置 及JdbcTemplate的使用 通过XML配置文件注入各种需要对象的操作 来完成数据库添加Add()方法
- android使用pull解析器读写xml文件…
- XML 文件的读写操作
- JDom操作XML文件读写
- java学习:使用dom4j读写xml文件
- Java_xml_dom4j对xml文件增删读写操作
- UNICODE模式下使用rapidxml写XML文件
- .NET下使用C#操作XML文件
- C#不使用DataSet操作XML,XmlDocument读写xml所有节点及读取xml节点的数据总结
- QSettings使用二(普通ini文件的读写:实际操作)
- 使用字符/字节流对文件进行读写操作
- UE4使用第三方库读写xml文件
- 使用dom4j 读写xml文件
- Qt 使用QDomDocument操作xml文件
- Windows下使用scapy+python2.7实现对pcap文件的读写操作