rapidxml:(内容源自网络)
一个项目对xml的操作是很常见的,在网上找了好久,经过大家一致认为,rapidxml在解析xml上占有绝对优势(bug需要在用的时候发觉)。
库可以配置宏:
RAPIDXML_NO_EXCEPTIONS //不使用异常
RAPIDXML_STATIC_POOL_SIZE //内存池大小默认(64 * 1024)
一般都不用改
rapidxml.hpp:
只要文件 实现内存池 解析string 异常处理
xml_base 基数节点类
xml_attribute 文本类
xml_node 节点类
xml_document 文档类
rapidxml_iterators.hpp:
提供两个迭代器类:node_iterator, attribute_iterator
rapidxml_print.hpp:
提供跟字符串,流对象的装换函数
rapidxml_utils.hpp:
提供一个file用来读取文件使用
跟两个计数函数 count_children,count_attributes
注意:需要说明的是rapidxml明显有一个bug,那就是append_node(doc.allocate_node(node_element,"h","0"));的时候并不考虑该对象是否存在!
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);
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 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>
读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<0>(fdoc.data());
std::cout<<doc.name()<<std::endl;
//! 获取根节点
xml_node<>* root = doc.first_node();
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;
}
结果:
<?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>