博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Quick notes on how to use RapidXML
阅读量:6230 次
发布时间:2019-06-21

本文共 3774 字,大约阅读时间需要 12 分钟。

There’s a C++ XML library called  which is perfect for most non-enterprise uses of XML. I wouldn’t call this a tutorial, but I hope this ends up helping someone. The documentation isn’t very explicit on how to output an XML declaration, for example.

How to create your XML from scratch and then output this XML into a string, with an XML declaration:

 
using namespace rapidxml; xml_document<> doc; // xml declaration xml_node<>* decl = doc.allocate_node(node_declaration); decl->append_attribute(doc.allocate_attribute("version", "1.0")); decl->append_attribute(doc.allocate_attribute("encoding", "utf-8")); doc.append_node(decl); // root node xml_node<>* root = doc.allocate_node(node_element, "rootnode"); root->append_attribute(doc.allocate_attribute("version", "1.0")); root->append_attribute(doc.allocate_attribute("type", "example")); doc.append_node(root); // child node xml_node<>* child = doc.allocate_node(node_element, "childnode"); root->append_node(child); std::string xml_as_string; // watch for name collisions here, print() is a very common function name! print(std::back_inserter(xml_as_string), doc); // xml_as_string now contains the XML in string form, indented // (in all its angle bracket glory) std::string xml_no_indent; // print_no_indenting is the only flag that print() knows about print(std::back_inserter(xml_as_string), doc, print_no_indenting); // xml_no_indent now contains non-indented XML

Parsing and traversing an XML document like this one:

 
   
   
 
 
 
void traverse_xml(std::string input_xml) {
    // (input_xml contains the above XML)     // make a safe-to-modify copy of input_xml     // (you should never modify the contents of an std::string directly)     vector
xml_copy(input_xml.begin(), input_xml.end());     xml_copy.push_back('\0');     // only use xml_copy from here on!     xml_document<> doc;     // we are choosing to parse the XML declaration     // parse_no_data_nodes prevents RapidXML from using the somewhat surprising     // behavior of having both values and data nodes, and having data nodes take     // precedence over values when printing     // >>> note that this will skip parsing of CDATA nodes <<<     doc.parse
(&xml_copy[0]);     // alternatively, use one of the two commented lines below to parse CDATA nodes,     // but please note the above caveat about surprising interactions between     // values and data nodes (also read http://www.ffuts.org/blog/a-rapidxml-gotcha/)     // if you use one of these two declarations try to use data nodes exclusively and     // avoid using value()     //doc.parse
(&xml_copy[0]); // just get the XML declaration     //doc.parse
(&xml_copy[0]); // parses everything (slowest)     // since we have parsed the XML declaration, it is the first node     // (otherwise the first node would be our root node)     string encoding = doc.first_node()->first_attribute("encoding")->value();     // encoding == "utf-8"     // we didn't keep track of our previous traversal, so let's start again     // we can match nodes by name, skipping the xml declaration entirely     xml_node<>* cur_node = doc.first_node("rootnode");     string rootnode_type = cur_node->first_attribute("type")->value();     // rootnode_type == "example"     // go straight to the first evendeepernode     cur_node = cur_node->first_node("childnode")->first_node("evendeepernode");     string attr2 = cur_node->first_attribute("attr2")->value();     // attr2 == "dog"     // and then to the second evendeepernode     cur_node = cur_node->next_sibling("evendeepernode");     attr2 = cur_node->first_attribute("attr2")->value();     // now attr2 == "wolf" }

转载地址:http://myana.baihongyu.com/

你可能感兴趣的文章
Lombok
查看>>
Single Number II
查看>>
由于一个老熟人对架构的一句话而拉黑了他。
查看>>
Mysql第八天 分区与分表
查看>>
CF 558A(Lala Land and Apple Trees-暴力)
查看>>
关于继承Fragment后重写构造方法而产生的错误
查看>>
2017-5-7 账号激活 权限设置 销售 仓库 财务三大模块
查看>>
datepicker插件的使用
查看>>
用户定义的变量+HTTP Cookie 管理器组合实现接口关联+问题处理
查看>>
linux中查找文件中的内容
查看>>
【C#学习笔记】调用C++生成的DLL
查看>>
Java:类与继承
查看>>
jQuery带tab切换搜索框样式代码
查看>>
jquery如何获得页面元素的坐标值
查看>>
《程序是怎样跑起来的》读书笔记——第六章 亲自尝试压缩数据
查看>>
poj1189
查看>>
AIM Tech Round 4 (Div. 2)
查看>>
JMeter介绍(一)
查看>>
自己实现字符串转整数(不使用JDK的字符串转整数的方法)
查看>>
虚拟化知识点
查看>>