日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

rapidxml使用

發布時間:2024/8/1 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 rapidxml使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

以前都是用tinyxml,這次開發中解析xml配置文件像嘗試一下rapidxml,據說效率很高。。。

RapidXml Manual:?http://rapidxml.sourceforge.net/manual.html

RapidXml是一個使用C++編寫的XML DOM解析工具包,整個解析工具包包含在一個頭文件中,所以使用時不用編譯也不用連接。只要包含rapidxml中的三個頭文件即可。

RapidXml 試圖成為最快的 XML DOM 解析工具包,同時保證解析結果的可用性、可移植性以及與 W3C 標準的兼容性。在操作同一數據時,其解析速度接近于 strlen() 函數。

以下代碼使用RapidXml解析一段以0結束的字符串text:

1 using namespace rapidxml; 2 xml_document<> doc; // character type defaults to char 3 doc.parse<0>(text); // 0 means default parse flags

其中,doc為解析得到的DOM tree的根節點。由于所有的RapidXml接口都包含在rapixml,所以用戶需要使用這個名字空間。類xml_document代表了DOM結構的根,它公開繼承了xml_node和memory_pool。xml_document::parse()的模板參數用來標識解析標志,使用它可以對解析器的行為進行調整(這里我也不太明白,調整什么?)。這個標志必須是編譯時的常數。

  • Accessing?DOM Tree:

使用xml_node和xml_attribute類中的方法訪問DOM tree。

1 cout << "Name of my first node is: " << doc.first_node()->name() << "\n"; 2 xml_node<> *node = doc.first_node("foobar"); 3 cout << "Node foobar has value " << node->value() << "\n"; 4 for (xml_attribute<> *attr = node->first_attribute(); 5 attr; attr = attr->next_attribute()) 6 { 7 cout << "Node foobar has attribute " << attr->name() << " "; 8 cout << "with value " << attr->value() << "\n"; 9 }
  • Modifying DOM Tree:

下例為創建一個HTML文檔,它唯一的內容是一個google.com的鏈接(?<a href=google.com>Google</a>):

xml_document<> doc; xml_node<> *node = doc.allocate_node(node_element, "a", "Google"); doc.append_node(node); xml_attribute<> *attr = doc.allocate_attribute("href", "google.com"); node->append_attribute(attr);

nodes和attributes并不真正擁有文章中節點和屬性的名字及值,因為它們只是存儲了指向源文中某個位置的指針。所以,當為一個節點分配名字和值的時候,必須確保待這些字符串有合適的生命周期。最簡單的方法是從xml_document memory pool中分配字符串。當然,在上面例子中沒有必要這么做,因為這里使用了字符常量。下面的代碼使用了memory_pool::allocate_string()方法分配節點名字(這樣它將和文檔具有相同的生命周期)給新的節點:

xml_document<> doc; char *node_name = doc.allocate_string(name); // Allocate string and copy name into it xml_node<> *node = doc.allocate_node(node_element, node_name); // Set node name to node_name
  • Printing XML

?將xml_document和xml_node的對象寫入一XML的string里,可以使用在rapidxml_print.hpp頭文件中定義的print()函數或者操作符<<。

using namespace rapidxml; xml_document<> doc; // character type defaults to char // ... some code to fill the document// Print to stream using operator << std::cout << doc; // Print to stream using print function, specifying printing flags print(std::cout, doc, 0); // 0 means default printing flags// Print to string using output iterator std::string s; print(std::back_inserter(s), doc, 0);// Print to memory buffer using output iterator char buffer[4096]; // You are responsible for making the buffer large enough! char *end = print(buffer, doc, 0); // end contains pointer to character after last printed character *end = 0; // Add string terminator after XML

?

包含必要的頭文件
#include "rapidxml.hpp"??
創建文檔對象
rapidxml::xml_document<char> doc;??
分析xml字符串,要求以'\0'結尾
std::string str(...);
doc.parse<0>(const_cast<char *>(str.c_str()));??
獲取節點
rapidxml::xml_node<char> * node = doc.first_node("node name");??
遍歷所有節點
for(rapidxml::xml_node<char> * node = parent_node->first_node("node name");
??? node != NULL;
??? node = node->next_sibling())
{
??? ...
}
??
遍歷所有屬性
for(rapidxml::xml_attribute<char> * attr = node->first_attribute("node name");
??? attr != NULL;
??? attr = attr->next_attribute())
{
??? ...
}??
獲取屬性值
char * value = attr->value();?

?

  • //?Load?file?to?buffer.??
  • rapid::xml_file<>?fileLoad("test.xml");??
  • char*?content?=?fileLoad.data();??
  • if?(content)?{??
  • ????//?Load?content?to?rapid.??
  • ????rapid::xml_document<>?docParse;??
  • ????docParse.parse<0>(content);??
  • ??
  • ????//?Parse?node.??
  • ????rapid::xml_node<>*?node_root?=?docParse.first_node("item");??
  • ????...???
  • } ?
  • rapid::xml_docmenet<>?docSave;??
  • ??
  • //?Generate?root?node.??
  • rapid::xml_node<>*?node_root?=?docSave.alloc_node(docSave.allocate_string("item"));??
  • ...??
  • ??
  • //?Save?to?file.??
  • std::ofstream?fileSave;??
  • fileSave?<<?docSave; ?
  • ?

    ?

    轉載于:https://www.cnblogs.com/kex1n/p/3190394.html

    總結

    以上是生活随笔為你收集整理的rapidxml使用的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。