rapidxml是一个快速的xml库,由C++模板实现的高效率xml解析库,同时也是boost库的property_tree的内置解析库。
当使用rapidxml时,只需要把rapidxml.hpp 、 rapidxml_print.hpp 和
rapidxml_utils.hpp 三个文件拷贝到你的工程目录下,就可以了。
一、xml的创建
1.xml创建并打印
代码执行时发现的错误:
bool Create(std::string filename)
{
//DOM
rapidxml::xml_document<>doc;
2.node_declaration
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);
下面代码出问题:有如下图片显示的错误
std::cout << doc;// 无法编译通过,为什么?
std::ofstream outfile(filename, std::ios::out);
if (outfile)
{
下面代码出问题:有如下图片显示的错误
char* end = rapidxml::print(buff, doc, 0);//无法编译通过
//std::cout << buff << std::endl;
return true;
}
return false;
}
创建时遇到的问题:
1.打印DOM的一个节点到缓冲区失败
2.打印DOM的一个节点到buff失败
解决办法:
在用这些函数的函数前 加个此函数的声明:
下面的注释都是后加的,解决了找不到的问题。
/*template<class OutIt, class Ch>
inline OutIt print_children(OutIt out, const xml_node<Ch>* node, int flags, int indent);
template<class OutIt, class Ch>
inline OutIt print_element_node(OutIt out, const xml_node<Ch>* node, int flags, int indent);
template<class OutIt, class Ch>
inline OutIt print_data_node(OutIt out, const xml_node<Ch>* node, int flags, int indent);
template<class OutIt, class Ch>
inline OutIt print_cdata_node(OutIt out, const xml_node<Ch>* node, int flags, int indent);
template<class OutIt, class Ch>
inline OutIt print_declaration_node(OutIt out, const xml_node<Ch>* node, int flags, int indent);
template<class OutIt, class Ch>
inline OutIt print_comment_node(OutIt out, const xml_node<Ch>* node, int flags, int indent);
template<class OutIt, class Ch>
inline OutIt print_doctype_node(OutIt out, const xml_node<Ch>* node, int flags, int indent);
template<class OutIt, class Ch>
inline OutIt print_pi_node(OutIt out, const xml_node<Ch>* node, int flags, int indent);*/
template<class OutIt, class Ch>
inline OutIt print_node(OutIt out, const xml_node<Ch> *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;
}
2.创建自己的数据输出到xml文件
步骤:
1.创建各个节点,并设置属性,注意控制节点之间的父子关系;
2.创建输出流ofstream,用rapidxml::print方法将doc数据输入到buff中;
3.用outfile将buff数据导入到文件中。
bool Create(std::string filename)
{
//DOM
std:: vector<std::string>::iterator it;
rapidxml::xml_document<>doc;
std::vector<std::string>tmp;
tmp.push_back("Count");
tmp.push_back("Step");
tmp.push_back("Record");
tmp.push_back("Record");
std::vector<std::string>tmp1;
tmp1.push_back("10");
tmp1.push_back("100");
tmp1.push_back("1000");
tmp1.push_back("10000");
2.node_declaration
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<>* Info = doc.allocate_node(rapidxml::node_element, "Info");
doc.append_node(Info);
rapidxml::xml_node<>* Settings = doc.allocate_node(rapidxml::node_element, "Settings");
Info->append_node(Settings);
std::vector<std::string>::iterator it1 = tmp1.begin();
for (it = tmp.begin(); it != tmp.end(); it++) {
rapidxml::xml_node<>* Count = doc.allocate_node(rapidxml::node_element,it->c_str());
Count->append_attribute(doc.allocate_attribute("value", it1->c_str()));
Settings->append_node(Count);
it1++;
}
//analysis->append_node()
rapidxml::xml_node<>* InstanceInfo = doc.allocate_node(rapidxml::node_element,"InstanceInfo");
Info->append_node(InstanceInfo);
for (int i = 0; i < 3; i++)
{
rapidxml::xml_node<>* Instance = doc.allocate_node(rapidxml::node_element,"Instance");
InstanceInfo->append_node(Instance);
Instance->append_attribute(doc.allocate_attribute("id","1"));
Instance->append_attribute(doc.allocate_attribute("name", "xxx"));
Instance->append_attribute(doc.allocate_attribute("fun", "sin"));
for (int j = 0; j < 2; j++)
{
rapidxml::xml_node<>* Parameter = doc.allocate_node(rapidxml::node_element, "Parameter");
Instance->append_node(Parameter);
Parameter->append_attribute(doc.allocate_attribute("name", "own"));
Parameter->append_attribute(doc.allocate_attribute("init", "1"));
}
}
std::cout << doc;// error 为什么?
std::ofstream outfile(filename, std::ios::out);
if (outfile)
{
char* end = rapidxml::print(buff, doc, 0);
*end = 0;
outfile << buff; //
outfile.close();
}
return true;
}
int main()
{
const char* file_name = "xxx.xml";
bool res = Create(file_name);
if (!res)
{
return -1;
}
return 0;
}
成功输出数据到文件中
二、读取xml格式到buff
/*这几个文件都要在项目文件夹中,才可以调用库文件*/
#include "rapidxml_print.hpp"
#include "rapidxml_utils.hpp"
//#include "rapidxml_iterators.hpp"
//#include "rapidxml_iterators.hpp"
using namespace rapidxml;
static const int buff_len = 2048;
static char buff[buff_len] = { 0 };
bool Read()
{
memset(buff, 0,buff_len);
std::string filename = "demo.xml";
//实例化文件读取流
std::ifstream infile(filename, std::ios::in);
if (!infile)
{
std::cout << "file stream instance error!" << std::endl;
return false;
}
//从流中读到buff
infile.read(buff, buff_len);
std::cout << buff << std::endl;
}
int main()
{
bool res = Read();
if (!res)
{
return -1;
}
return 0;
}
注意此时的xml文件是否在工作目录下,通过右键项目->属性->调试,查看工作路径是否存在xml文件,路径对修改
例如:projectDir 改为OutDir 查看路径是否正确。