微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!
RapidXml使用方法
分类:XML作者:编程之家
一、写xml 文件
- #include<iostream>
- #include"rapidxml/rapidxml.hpp"
- #include"rapidxml/rapidxml_utils.hpp"
- #include"rapidxml/rapidxml_print.hpp"
-
- usingnamespacerapidxml;
- intmain()
- {
- xml_document<>doc;
- xml_node<>*rot=doc.allocate_node(rapidxml::node_pi,doc.allocate_string("xmlversion='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"));
- "blue","alpha","1.0"));
- xml_node<>*size=doc.allocate_node(node_element,"size",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> 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","screenmode");
- mode->append_attribute(doc.allocate_attribute("fullscreen","false"));
- node->append_node(mode);
- std::stringtext;
- rapidxml::print(std::back_inserter(text),doc,0);
- std::cout<<text<<std::endl;
- std::ofstreamout("config.xml");
- out<<doc;
- system("PAUSE");
- returnEXIT_SUCCESS;
- }
生成的xml如下
<?xmlversion="1.0"encoding="utf-8"?>
- -<config>
- -color>
- red>0.1</ greenbluealpha>1.0sizex>640y>480modefullscreen="false">screenmodemode>
这里需要注意的是:rapidxml为了追求性能,减少内存拷贝,就尽可能的通过指针(内存地址)来访问用户的变量;这就对用户提出了要求:必须保证变量的生存周期,如果变量被销毁了,rapidxml就会访问无效的内存地址,引发不可控的后果。
可见下例:
rapidxml::xml_document<> doc;
void addNode(std::string value)
{
rapidxml::xml_node<>* root = doc.allocate_node(rapidxml::node_element,"unregister_context");
doc.append_node(root);
root->append_node(doc.allocate_node(rapidxml::node_element,"who_register",value.c_str()));
}
这样插入是有问题的,value是临时变量,函数执行完之后就被释放了,正确的方法如下:
rapidxml::xml_document<> doc;
void addNode(std::string value)
{
rapidxml::xml_node<>* root = doc.allocate_node(rapidxml::node_element,doc.allocate_string(value.c_str())));
}
待插入的值"变量value"是作为参数传递进来的,是临时变量。rapidxml为了追求极致性能,在append_node()函数中是直接通过指针来访问value变量的,并没有进行内存拷贝--因此rapidxml在这里提出了一个隐晦的前提条件:在xml对象doc的生命周期内,必须保证"变量value"能够被正常访问。
写文件例子2:
#include<string>
- #include<iostream>
- #include<fstream>
- namespacerapidxml;
- namespacestd;
- intmain(intargc,char*argv[])
- xml_document<>doc;
- chara[]="<top>"
-
- "<name>tangqiang</name>"
- "<age>22</age>"
- "</top>";
- char*p=a;
- doc.parse<0>(p);
- xml_node<>*node=doc.first_node();
- cout<<(node->name())<<endl;
- node=node->first_node();
- while(node){
- cout<<node->name()<<node->value()<<endl;
- node=node->next_sibling();
- }
- ofstreamout("test.xml");
- out<<doc;
- out.close();
- system("pause");
- return0;
- }
生成的xml如下
topname>tangqiangage>22>
二、读取xml文件
file<>fdoc("config.xml");
- std::cout<<fdoc.data()<<std::endl;
- doc.parse<0>(fdoc.data());
- std::cout<<doc.name()<<std::endl;
- //!获取根节点
- xml_node<>*root=doc.first_node();
- std::cout<<root->name()<<std::endl;
- //!获取根节点第一个节点
- xml_node<>*node1=root->first_node();
- std::cout<<node1->name()<<std::endl;
- xml_node<>*node11=node1->first_node();
- std::cout<<node11->name()<<std::endl;
- std::cout<<node11->value()<<std::endl;
- //!添加之后再次保存
-
- //那就是append_node(doc.allocate_node(node_element,"h","0"));的时候并不考虑该对象是否存在!
- xml_node<>*size=root->first_node("size");
- "w","0"));
- "h","0"));
- std::stringtext;
- rapidxml::print(std::back_inserter(text),0);
- std::cout<<text<<std::endl;
- std::ofstreamout("config.xml");
- out<<doc;
- system("PAUSE");
- returnEXIT_SUCCESS;
- }
生成的xml为
w>0h>
三、删除节点
#include"rapidxml/rapidxml.hpp"
- #include"rapidxml/rapidxml_utils.hpp"
- #include"rapidxml/rapidxml_print.hpp"
- xml_document<>doc;
- doc.parse<0>(fdoc.data());
- xml_node<>*sec=root->first_node();
- root->remove_node(sec);
- text="删除一个节点\r\n";
- root->remove_all_nodes();
- text="删除所有节点\r\n";
- std::ofstreamout("test.xml");
- system("pause");
- return0;
- }
输出信息如下:
删除一个节点
- 删除所有节点
- />
四、编辑节点信息
暂时找到的编辑方法就是先删除再增加
xml_node<>*delnode=root->first_node("color");
- root->remove_node(delnode);
- //
- xml_node<>*lnode=root->first_node("size");
- xml_node<>*mynode=doc.allocate_node(node_element,"address","河北");
- root->insert_node(lnode,mynode);
- std::ofstreamout("version.xml");
- }
输出如下:
address>河北>
五、遍历所有节点
for(rapidxml::xml_node<char>*node=parent_node->first_node("nodename");
- node!=NULL;
- node=node->next_sibling())
- {
- ...
- }
六、遍历所有属性
for(rapidxml::xml_attribute<char>*attr=node->first_attribute("nodename");
- 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<Ch> ...
在这里找到了解决方法。
经查,原来print_node()函数被其他函数调用,但在却未定义(在被调用函数后定义了),所以解决方法为把print_node()函数移到print_children(),print_element_node() 等函数的后面。在原定义处就留一个函数声明就行。
具体diff文件如下。
Index:rapidxml_print.hpp
- ===================================================================
- ---rapidxml_print.hpp(revision2025)
- +++rapidxml_print.hpp(revision2080)
- @@-101,68+101,9@@
- ///////////////////////////////////////////////////////////////////////////
- //Internalprintingoperations
- -
- -//Printnode
- +
- template<classOutIt,classCh>
- -inlineOutItprint_node(OutItout,constxml_node<Ch>*node,intflags,intindent)
- -{
- -//Printpropernodetype
- -switch(node->type())
- -{
- -
- -//Document
- -casenode_document:
- -out=print_children(out,indent);
- -break;
- -//Element
- -casenode_element:
- -out=print_element_node(out,indent);
- -break;
- -//Data
- -casenode_data:
- -out=print_data_node(out,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> -//CDATA
- -casenode_cdata:
- -out=print_cdata_node(out,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> -//Declaration
- -casenode_declaration:
- -out=print_declaration_node(out,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> -//Comment
- -casenode_comment:
- -out=print_comment_node(out,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> -//Doctype
- -casenode_doctype:
- -out=print_doctype_node(out,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> -//Pi
- -casenode_pi:
- -out=print_pi_node(out,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> -//UnkNown
- -default:
- -assert(0);
- -}
- -//Ifindentingnotdisabled,addlinebreakafternode
- -if(!(flags&print_no_indenting))
- -*out=Ch('\n'),++out;
- -//Returnmodifiediterator
- -returnout;
- +inlineOutItprint_node(OutItout,intindent);
- //Printchildrenofthenode
- template<classOutIt,classCh>
- @@-372,7+313,69@@
- *out=Ch('>'),248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> returnout;
- }
- +
- +//Printnode
- +template<classOutIt,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> +inlineOutItprint_node(OutItout,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> +{
- +//Printpropernodetype
- +switch(node->type())
- +{
- +//Document
- +casenode_document:
- +out=print_children(out,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> +break;
- +//Element
- +casenode_element:
- +out=print_element_node(out,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> +break;
- +//Data
- +casenode_data:
- +out=print_data_node(out,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> +//CDATA
- +casenode_cdata:
- +out=print_cdata_node(out,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> +//Declaration
- +casenode_declaration:
- +out=print_declaration_node(out,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> +//Comment
- +casenode_comment:
- +out=print_comment_node(out,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> +//Doctype
- +casenode_doctype:
- +out=print_doctype_node(out,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> +//Pi
- +casenode_pi:
- +out=print_pi_node(out,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> +//UnkNown
- +default:
- +assert(0);
- +}
- +//Ifindentingnotdisabled,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> +if(!(flags&print_no_indenting))
- +*out=Ch('\n'),108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> +//Returnmodifiediterator
- +returnout;
- //!\endcond
-
原文地址:https://www.jb51.cc/xml/299055.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。