rapidxml的常见读写操作
rapidxml官网地址:http://rapidxml.sourceforge.net/
rapidxml只包含4个hpp头文件,把这四个头文件放到项目中,即可使用rapidxml
#include <iostream> #include <string> #include <fstream> #include "rapidxml.hpp" #include "rapidxml_print.hpp" static const int buf_len = 1024; static char buf[buf_len] = { 0 };
void create(const char * file_name) { rapidxml::xml_document<> doc; // 声明 rapidxml::xml_node<>* declaration = doc.allocate_node(rapidxml::node_declaration); declaration->append_attribute(doc.allocate_attribute("version", "1.0")); declaration->append_attribute(doc.allocate_attribute("encoding", "utf-8")); doc.append_node(declaration); // 根节点 rapidxml::xml_node<>* root = doc.allocate_node(rapidxml::node_element, "root"); doc.append_node(root); // 注释节点1 rapidxml::xml_node<>* comment1 = doc.allocate_node(rapidxml::node_comment, 0, "all students info"); root->append_node(comment1); // 普通节点1 rapidxml::xml_node<>* students = doc.allocate_node(rapidxml::node_element, "students"); for (int i = 0; i < 10; ++i) { rapidxml::xml_node<>* one_student = doc.allocate_node(rapidxml::node_element, "student"); sprintf(buf, "student_%02d", i); // doc.allocate_string 的作用是将源字符串深拷贝一份 one_student->append_attribute(doc.allocate_attribute("name", doc.allocate_string(buf))); one_student->append_attribute(doc.allocate_attribute("score", doc.allocate_string(std::to_string(100 - i).c_str()))); students->append_node(one_student); } root->append_node(students); // 注释节点2 rapidxml::xml_node<>* comment2 = doc.allocate_node(rapidxml::node_comment, 0, "all books info"); root->append_node(comment2); // 普通节点2 rapidxml::xml_node<>* books = doc.allocate_node(rapidxml::node_element, "books"); for (int i = 0; i < 10; ++i) { rapidxml::xml_node<>* one_book = doc.allocate_node(rapidxml::node_element, "book"); sprintf(buf, "book_%02d", i); // doc.allocate_string 的作用是将源字符串深拷贝一份 one_book->append_attribute(doc.allocate_attribute("name", doc.allocate_string(buf))); one_book->append_attribute(doc.allocate_attribute("price", doc.allocate_string(std::to_string(50 - i).c_str()))); books->append_node(one_book); } root->append_node(books); std::ofstream outfile(file_name, std::ios::out); if (outfile) { char *end = rapidxml::print(buf, doc, 0); *end = 0; outfile << buf; outfile.close(); } } void parse(const char * file_name) { std::ifstream infile(file_name, std::ios::in); if (!infile) { return; } infile.read(buf, buf_len); std::cout << buf << std::endl; rapidxml::xml_document<> doc; doc.parse<0>(buf); // 取得根节点 rapidxml::xml_node<> *root = doc.first_node("root"); // 遍历students的子节点 for (rapidxml::xml_node<> * node = root->first_node("students")->first_node(); node; node = node->next_sibling()) { std::cout << node->first_attribute("name")->value() << ", " << node->first_attribute("score")->value() << std::endl; } // 遍历books的子节点 for (rapidxml::xml_node<> * node = root->first_node("books")->first_node(); node; node = node->next_sibling()) { std::cout << node->first_attribute("name")->value() << ", " << node->first_attribute("price")->value() << std::endl; } } int main() { const char * file_name = "info.xml"; create(file_name); parse(file_name); return 0; }
生成的示例文件:
<?xml version="1.0" encoding="utf-8"?> <root> <!--all students info--> <students> <student name="student_00" score="100"/> <student name="student_01" score="99"/> <student name="student_02" score="98"/> <student name="student_03" score="97"/> <student name="student_04" score="96"/> <student name="student_05" score="95"/> <student name="student_06" score="94"/> <student name="student_07" score="93"/> <student name="student_08" score="92"/> <student name="student_09" score="91"/> </students> <!--all books info--> <books> <book name="book_00" price="50"/> <book name="book_01" price="49"/> <book name="book_02" price="48"/> <book name="book_03" price="47"/> <book name="book_04" price="46"/> <book name="book_05" price="45"/> <book name="book_06" price="44"/> <book name="book_07" price="43"/> <book name="book_08" price="42"/> <book name="book_09" price="41"/> </books> </root>