Write
#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 WriteXml(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);
rapidxml::xml_node<>* comment1 = doc.allocate_node(rapidxml::node_comment, 0, "all students info");
root->append_node(comment1);
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_s(buf, "student_%02d", i);
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);
rapidxml::xml_node<>* comment2 = doc.allocate_node(rapidxml::node_comment, 0, "all books info");
root->append_node(comment2);
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_s(buf, "book_%02d", i);
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);
char *end = rapidxml::print(buf, doc, 0);
*end = 0;
std::cout << buf;
std::ofstream outfile(file_name, std::ios::out);
if (outfile)
{
outfile << buf;
outfile.close();
}
}
int main()
{
WriteXml("info.xml");
return 0;
}
Read
#include <iostream>
#include <fstream>
#include "rapidxml.hpp"
static const int buf_len = 1024;
static char buf[buf_len] = { 0 };
void ReadXml(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");
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;
}
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()
{
ReadXml("info.xml");
return 0;
}