RapidXml使用方法

前端之家收集整理的这篇文章主要介绍了RapidXml使用方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

一、写xml 文件

  1. #include<iostream>
  2. #include"rapidxml/rapidxml.hpp"
  3. #include"rapidxml/rapidxml_utils.hpp"
  4. @H_403_38@ #include"rapidxml/rapidxml_print.hpp"
  5. @H_403_38@ usingnamespacerapidxml;
  6. intmain()
  7. {
  8. @H_403_38@ xml_document<>doc;
  9. xml_node<>*rot=doc.allocate_node(rapidxml::node_pi,doc.allocate_string("xmlversion='1.0'encoding='utf-8'"));
  10. @H_403_38@ doc.append_node(rot);
  11. xml_node<>*node=doc.allocate_node(node_element,"config","information");
  12. @H_403_38@ xml_node<>*color=doc.allocate_node(node_element,"color",NULL);
  13. doc.append_node(node);
  14. @H_403_38@ node->append_node(color);
  15. color->append_node(doc.allocate_node(node_element,"red","0.1"));
  16. @H_403_38@ color->append_node(doc.allocate_node(node_element,"green","0.1"));
  17. "blue","alpha","1.0"));
  18. 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"));
  19. @H_403_38@ size->append_node(doc.allocate_node(node_element,"y","480"));
  20. node->append_node(size);
  21. @H_403_38@
  22. xml_node<>*mode=doc.allocate_node(rapidxml::node_element,"mode","screenmode");
  23. @H_403_38@ mode->append_attribute(doc.allocate_attribute("fullscreen","false"));
  24. node->append_node(mode);
  25. std::stringtext;
  26. @H_403_38@ rapidxml::print(std::back_inserter(text),doc,0);
  27. std::cout<<text<<std::endl;
  28. std::ofstreamout("config.xml");
  29. out<<doc;
  30. system("PAUSE");
  31. returnEXIT_SUCCESS;
  32. }


生成的xml如下

[html] copy
    <?xmlversion="1.0"encoding="utf-8"?> @H_403_38@ -<config>
  1. -color>
  2. @H_403_38@ 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:

copy
    #include<string> @H_403_38@ #include<iostream>
  1. #include<fstream>
  2. namespacerapidxml;
  3. namespacestd;
  4. intmain(intargc,char*argv[])
  5. xml_document<>doc;//是解析器
  6. chara[]="<top>"//如果单独传,就不能加上xml的头部信息,
  7. //否则会报错
  8. @H_403_38@ "<name>tangqiang</name>"
  9. "<age>22</age>"
  10. @H_403_38@ "</top>";
  11. char*p=a;
  12. @H_403_38@ doc.parse<0>(p);
  13. xml_node<>*node=doc.first_node();//去顶级结点
  14. cout<<(node->name())<<endl;
  15. @H_403_38@ node=node->first_node();
  16. while(node){
  17. @H_403_38@ cout<<node->name()<<node->value()<<endl;//name()value()返回的字符串不会去掉首尾的空白字符
  18. node=node->next_sibling();
  19. @H_403_38@ }
  20. ofstreamout("test.xml");//ofstream默认时,如果文件存在则会覆盖原来的内容,不存在则会新建
  21. out<<doc;//doc这样输出时在目标文件中不会有xml头信息---<?xmlversion='1.0'encoding='utf-8'>
  22. @H_403_38@ out.close();
  23. system("pause");
  24. return0;
  25. }

生成的xml如下

copy
    topname>tangqiangage>22>


二、读取xml文件

copy
    file<>fdoc("config.xml");
  1. std::cout<<fdoc.data()<<std::endl;
  2. doc.parse<0>(fdoc.data());
  3. std::cout<<doc.name()<<std::endl;
  4. //!获取根节点
  5. @H_403_38@ xml_node<>*root=doc.first_node();
  6. std::cout<<root->name()<<std::endl;
  7. //!获取根节点第一个节点
  8. @H_403_38@ xml_node<>*node1=root->first_node();
  9. std::cout<<node1->name()<<std::endl;
  10. xml_node<>*node11=node1->first_node();
  11. @H_403_38@ std::cout<<node11->name()<<std::endl;
  12. std::cout<<node11->value()<<std::endl;
  13. //!添加之后再次保存
  14. @H_403_38@ //需要说明的是rapidxml明显有一个bug
  15. //那就是append_node(doc.allocate_node(node_element,"h","0"));的时候并不考虑该对象是否存在!
  16. @H_403_38@ xml_node<>*size=root->first_node("size");
  17. "w","0"));
  18. "h","0"));
  19. std::stringtext;
  20. rapidxml::print(std::back_inserter(text),0);
  21. std::cout<<text<<std::endl;
  22. std::ofstreamout("config.xml");
  23. @H_403_38@ out<<doc;
  24. system("PAUSE");
  25. returnEXIT_SUCCESS;
  26. @H_403_38@ }

生成的xml为

copy
    w>0h>


三、删除节点

copy
    #include"rapidxml/rapidxml.hpp" @H_403_38@ #include"rapidxml/rapidxml_utils.hpp"
  1. #include"rapidxml/rapidxml_print.hpp"
  2. xml_document<>doc;
  3. @H_403_38@ doc.parse<0>(fdoc.data());
  4. xml_node<>*sec=root->first_node();
  5. root->remove_node(sec);//移除根节点下的sec结点(包括该结点下所有结点)
  6. text="删除一个节点\r\n";
  7. root->remove_all_nodes();//移除根节点下所有结点
  8. @H_403_38@ text="删除所有节点\r\n";
  9. std::ofstreamout("test.xml");
  10. system("pause");
  11. return0;
  12. @H_403_38@ }

输出信息如下:

copy
    删除一个节点
  1. 删除所有节点
  2. />


四、编辑节点信息

暂时找到的编辑方法就是先删除增加

copy
    xml_node<>*delnode=root->first_node("color"); @H_403_38@ root->remove_node(delnode);//先删除address节点
  1. //
  2. @H_403_38@ xml_node<>*lnode=root->first_node("size");//找到post节点
  3. xml_node<>*mynode=doc.allocate_node(node_element,"address","河北");
  4. @H_403_38@ root->insert_node(lnode,mynode);
  5. std::ofstreamout("version.xml");
  6. }

输出如下:

copy
    address>河北>

五、遍历所有节点

copy
    for(rapidxml::xml_node<char>*node=parent_node->first_node("nodename"); @H_403_38@ node!=NULL;
  1. node=node->next_sibling())
  2. @H_403_38@ {
  3. ...
  4. @H_403_38@ }

六、遍历所有属性

copy
    for(rapidxml::xml_attribute<char>*attr=node->first_attribute("nodename"); @H_403_38@ attr!=NULL;
  1. attr=attr->next_attribute())
  2. char*value=attr->value();
  3. @H_403_38@ }


七、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文件如下。

[plain] copy
    Index:rapidxml_print.hpp @H_403_38@ ===================================================================
  1. ---rapidxml_print.hpp(revision2025)
  2. @H_403_38@ +++rapidxml_print.hpp(revision2080)
  3. @@-101,68+101,9@@
  4. ///////////////////////////////////////////////////////////////////////////
  5. @H_403_38@ //Internalprintingoperations
  6. -
  7. @H_403_38@ -//Printnode
  8. +
  9. @H_403_38@ template<classOutIt,classCh>
  10. -inlineOutItprint_node(OutItout,constxml_node<Ch>*node,intflags,intindent)
  11. @H_403_38@ -{
  12. -//Printpropernodetype
  13. @H_403_38@ -switch(node->type())
  14. -{
  15. @H_403_38@ -
  16. -//Document
  17. @H_403_38@ -casenode_document:
  18. -out=print_children(out,indent);
  19. @H_403_38@ -break;
  20. -//Element
  21. -casenode_element:
  22. @H_403_38@ -out=print_element_node(out,indent);
  23. -break;
  24. -//Data
  25. @H_403_38@ -casenode_data:
  26. -out=print_data_node(out,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> -//CDATA
  27. -casenode_cdata:
  28. @H_403_38@ -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 @H_403_38@ -casenode_declaration:
  29. -out=print_declaration_node(out,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> -//Comment
  30. -casenode_comment:
  31. @H_403_38@ -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 @H_403_38@ -casenode_doctype:
  32. -out=print_doctype_node(out,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> -//Pi
  33. -casenode_pi:
  34. @H_403_38@ -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 @H_403_38@ -default:
  35. -assert(0);
  36. -}
  37. -//Ifindentingnotdisabled,addlinebreakafternode
  38. @H_403_38@ -if(!(flags&print_no_indenting))
  39. -*out=Ch('\n'),++out;
  40. -//Returnmodifiediterator
  41. @H_403_38@ -returnout;
  42. +inlineOutItprint_node(OutItout,intindent);
  43. //Printchildrenofthenode
  44. template<classOutIt,classCh>
  45. @H_403_38@ @@-372,7+313,69@@
  46. *out=Ch('>'),248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> returnout;
  47. }
  48. @H_403_38@ +
  49. +//Printnode
  50. @H_403_38@ +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"> +{
  51. +//Printpropernodetype
  52. @H_403_38@ +switch(node->type())
  53. +{
  54. +//Document
  55. @H_403_38@ +casenode_document:
  56. +out=print_children(out,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> +break;
  57. +//Element
  58. +casenode_element:
  59. @H_403_38@ +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;
  60. +//Data
  61. @H_403_38@ +casenode_data:
  62. +out=print_data_node(out,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> +//CDATA
  63. +casenode_cdata:
  64. @H_403_38@ +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 @H_403_38@ +casenode_declaration:
  65. +out=print_declaration_node(out,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> +//Comment
  66. +casenode_comment:
  67. @H_403_38@ +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 @H_403_38@ +casenode_doctype:
  68. +out=print_doctype_node(out,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> +//Pi
  69. +casenode_pi:
  70. @H_403_38@ +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 @H_403_38@ +default:
  71. +assert(0);
  72. +}
  73. +//Ifindentingnotdisabled,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> +if(!(flags&print_no_indenting))
  74. +*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
  75. @H_403_38@ +returnout;
  76. //!\endcond


出处:http://www.jb51.cc/article/p-rjkkawye-gu.html

猜你在找的XML相关文章