RapidXml用法
2020-12-13 03:26
一、写xml 文件
#include#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
生成的xml例如以下-- - 0.1 0.1 0.1 1.0640 480 screen mode
写文件样例2:
#include#include #include #include "rapidxml/rapidxml.hpp" #include "rapidxml/rapidxml_utils.hpp" #include "rapidxml/rapidxml_print.hpp" using namespace rapidxml; using namespace std; int main(int argc, char* argv[]) { xml_document doc; //是解析器 char a[] = " "//假设单独传, 就不能加上xml的头部信息, //否则会报错 " "; char* p = a; doc.parse(p); xml_node* node = doc.first_node();//去顶级结点 cout name())first_node(); while (node) { cout name() value() next_sibling(); } ofstream out("test.xml");//ofstream 默认时,假设文件存在则会覆盖原来的内容,不存在则会新建 out out.close(); system("pause"); return 0; }tangqiang " "22" "
生成的xml例如以下tangqiang 22二、读取xml文件
#include#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 doc; doc.parse(fdoc.data()); std::cout* root = doc.first_node(); std::coutname()* node1 = root->first_node(); std::coutname()* node11 = node1->first_node(); std::coutname()value()* 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
生成的xml为0.1 0.1 0.1 1.0640 480 0 0 screen mode
三、删除节点
#include "rapidxml/rapidxml.hpp" #include "rapidxml/rapidxml_utils.hpp" #include "rapidxml/rapidxml_print.hpp" #includeusing namespace rapidxml; int main() { file fdoc("config.xml"); xml_document doc; doc.parse(fdoc.data()); std::string text; rapidxml::print(std::back_inserter(text), doc, 0); std::cout* root = doc.first_node(); xml_node* sec = root->first_node(); root->remove_node(sec); //移除根节点下的sec结点(包含该结点下全部结点) text="删除一个节点\r\n"; rapidxml::print(std::back_inserter(text), doc, 0); std::coutremove_all_nodes(); //移除根节点下全部结点 text="删除全部节点\r\n"; rapidxml::print(std::back_inserter(text), doc, 0); std::cout
输出信息例如以下:删除一个节点 0.1 0.1 0.1 1.0640 480 0 0 screen mode 删除全部节点 640 480 0 0 screen mode
四、编辑节点信息
临时找到的编辑方法就是先删除再添加?
#include "rapidxml/rapidxml.hpp" #include "rapidxml/rapidxml_utils.hpp" #include "rapidxml/rapidxml_print.hpp" #includeusing namespace rapidxml; int main() { file fdoc("config.xml"); std::cout doc; doc.parse(fdoc.data()); std::cout* root = doc.first_node(); xml_node* delnode = root->first_node("color"); root->remove_node(delnode);//先删除address节点 // xml_node* lnode = root->first_node("size");//找到post节点 xml_node* mynode=doc.allocate_node(node_element,"address","河北"); root->insert_node(lnode,mynode); std::string text; rapidxml::print(std::back_inserter(text),doc,0); std::cout
输出例如以下:0.1 0.1 0.1 1.0640 480 0 0 screen mode 河北 640 480 0 0 screen mode 五、遍历全部节点
for(rapidxml::xml_node* node = parent_node->first_node("node name"); node != NULL; node = node->next_sibling()) { ... }
六、遍历全部属性for(rapidxml::xml_attribute* attr = node->first_attribute("node name"); attr != NULL; attr = attr->next_attribute()) { char * value = attr->value(); }
七、gcc使用
-std=gnu++0x
编译rapidxml时会报错,错误信息大概例如以下...rapidxml_print.hpp:120:23: error:
call to function ‘print_element_node‘ thatis neither visible in the
template definition nor found byargument-dependent lookup
out = print_element_node(out, node, flags,indent);
^
...rapidxml_print.hpp:242:22: note:
‘print_element_node‘ should be declaredprior to the call site or in
namespace ‘rapidxml‘
inline OutIt print_element_node(OutIt out,const xml_node
... 在这里找到了解决方法。
经查,原来print_node()函数被其它函数调用,但在却没有定义(在被调用函数后定义了),所以解决方法为把print_node()函数移到print_children(), print_element_node() 等函数的后面。在原定义处就留一个函数声明即可。
详细diff文件例如以下。
Index: rapidxml_print.hpp =================================================================== --- rapidxml_print.hpp (revision 2025) +++ rapidxml_print.hpp (revision 2080) @@ -101,68 +101,9 @@ /////////////////////////////////////////////////////////////////////////// // Internal printing operations - - // Print node + template- inline OutIt print_node(OutIt out, const xml_node *node, int flags, int indent) - { - // Print proper node type - switch (node->type()) - { - - // Document - case node_document: - out = print_children(out, node, flags, indent); - break; - - // Element - case node_element: - out = print_element_node(out, node, flags, indent); - break; - - // Data - case node_data: - out = print_data_node(out, node, flags, indent); - break; - - // CDATA - case node_cdata: - out = print_cdata_node(out, node, flags, indent); - break; - - // Declaration - case node_declaration: - out = print_declaration_node(out, node, flags, indent); - break; - - // Comment - case node_comment: - out = print_comment_node(out, node, flags, indent); - break; - - // Doctype - case node_doctype: - out = print_doctype_node(out, node, flags, indent); - break; - - // Pi - case node_pi: - out = print_pi_node(out, node, flags, indent); - break; - - // Unknown - default: - assert(0); - break; - } - - // If indenting not disabled, add line break after node - if (!(flags & print_no_indenting)) - *out = Ch(‘\n‘), ++out; - - // Return modified iterator - return out; - } + inline OutIt print_node(OutIt out, const xml_node *node, int flags, int indent); // Print children of the node template @@ -372,7 +313,69 @@ *out = Ch(‘>‘), ++out; return out; } + + // Print node + template + inline OutIt print_node(OutIt out, const xml_node *node, int flags, int indent) + { + // Print proper node type + switch (node->type()) + { + // Document + case node_document: + out = print_children(out, node, flags, indent); + break; + + // Element + case node_element: + out = print_element_node(out, node, flags, indent); + break; + + // Data + case node_data: + out = print_data_node(out, node, flags, indent); + break; + + // CDATA + case node_cdata: + out = print_cdata_node(out, node, flags, indent); + break; + + // Declaration + case node_declaration: + out = print_declaration_node(out, node, flags, indent); + break; + + // Comment + case node_comment: + out = print_comment_node(out, node, flags, indent); + break; + + // Doctype + case node_doctype: + out = print_doctype_node(out, node, flags, indent); + break; + + // Pi + case node_pi: + out = print_pi_node(out, node, flags, indent); + break; + + // Unknown + default: + assert(0); + break; + } + + // If indenting not disabled, add line break after node + if (!(flags & print_no_indenting)) + *out = Ch(‘\n‘), ++out; + + // Return modified iterator + return out; + } + } //! \endcond