RapidXml使用方法

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

一、写xml 文件

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


生成的xml如下

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

生成的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. xml_node<>*root=doc.first_node();
  6. std::cout<<root->name()<<std::endl;
  7. //!获取根节点第一个节点
  8. xml_node<>*node1=root->first_node();
  9. std::cout<<node1->name()<<std::endl;
  10. xml_node<>*node11=node1->first_node();
  11. std::cout<<node11->name()<<std::endl;
  12. std::cout<<node11->value()<<std::endl;
  13. //!添加之后再次保存
  14. //需要说明的是rapidxml明显有一个bug
  15. //那就是append_node(doc.allocate_node(node_element,"h","0"));的时候并不考虑该对象是否存在!
  16. 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. out<<doc;
  24. system("PAUSE");
  25. returnEXIT_SUCCESS;
  26. }

生成的xml为

copy
    w>0h>


三、删除节点

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

输出信息如下:

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


四、编辑节点信息

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

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

输出如下:

copy
    address>河北>

五、遍历所有节点

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

六、遍历所有属性

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


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


出处:http://www.jb51.cc/article/p-rjkkawye-gu.html
原文链接:https://www.f2er.com/xml/299055.html

猜你在找的XML相关文章