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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

rapidxml 文件读写,增加删除节点

發(fā)布時(shí)間:2024/8/1 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 rapidxml 文件读写,增加删除节点 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

RapidXml是指 XML DOM解析工具包,是一個(gè)快速的讀寫(xiě)xml文件的庫(kù)文件(hpp)。本文旨在提供RapidXml文件讀寫(xiě)操作,以及對(duì)節(jié)點(diǎn)的增加、刪除、編譯提供一個(gè)測(cè)試用例,以免忘記。

1. 讀取XML

#include "rapidxml.hpp" #include "rapidxml_utils.hpp" #include "rapidxml_print.hpp" #include <vector> #include <string> #include <map> #include <set> #include <iostream> #include <sstream> #include <io.h>void str2int(int& int_tmp, const std::string& string_tmp) {std::stringstream stream(string_tmp);stream >> int_tmp; }void str2float(float& float_tmp, const std::string& string_tmp) {float_tmp = atof(string_tmp.c_str()); }void splitLabels(const std::string& labels, std::vector<std::string>& splitalbelres) {std::string result;std::stringstream input(labels);while (input >> result){splitalbelres.push_back(result);} }void readFunctionParam(rapidxml::xml_node<char>* FunctionNode) {rapidxml::xml_node<char>* DeteNode = FunctionNode->first_node("Detectors");if (DeteNode != nullptr){for (rapidxml::xml_node<char>* node = DeteNode->first_node("iter"); node != nullptr; node = node->next_sibling()){std::string name = "";std::string version = "";std::string label = "";float fConfidence = 0.0;int iShowWin = 0;int iIsStandard = 0;std::set<int> setUsefulLabel;rapidxml::xml_node<>* pName = node->first_node("name");if (pName != nullptr){name = pName->value();}rapidxml::xml_node<>* pVersion = node->first_node("version");if (pVersion != nullptr){version = pVersion->value();}rapidxml::xml_node<>* pConf = node->first_node("confidence");if (pConf != nullptr){str2float(fConfidence, pConf->value());}rapidxml::xml_node<>* pShowWin = node->first_node("bshowDebugWin");if (pShowWin != nullptr){str2int(iShowWin, pShowWin->value());}rapidxml::xml_node<>* pIsStandard = node->first_node("bStandard");if (pIsStandard != nullptr){str2int(iIsStandard, pIsStandard->value());}rapidxml::xml_node<>* pUsefulLabel = node->first_node("usefulLabel");if (pUsefulLabel != nullptr){label = pUsefulLabel->value();std::vector<std::string> splitlabelres;splitLabels(label, splitlabelres);for (int i = 0; i < splitlabelres.size(); i++){setUsefulLabel.insert(std::atoi(splitlabelres[i].c_str()));}}}rapidxml::xml_node<char>* pShowreportNode = FunctionNode->first_node("ShowReportWin");if (pShowreportNode != nullptr){int iShowReportWin = 0;str2int(iShowReportWin, pShowreportNode->value());}} }void ReadParam(const rapidxml::xml_document<>& pDoc) {//獲取根節(jié)點(diǎn)rapidxml::xml_node<char>* root = pDoc.first_node("config");//找到采樣幀率節(jié)點(diǎn)int iSampleFrequency;rapidxml::xml_node<>* samplefre = root->first_node("SampleFrequency");str2int(iSampleFrequency, samplefre->value());//找到功能節(jié)點(diǎn)std::string sFunctionName = "SMOKEFireDetection";rapidxml::xml_node<>* intrusion = root->first_node(sFunctionName.c_str());if (intrusion != nullptr){readFunctionParam(intrusion);}}void readXMLByFile(const std::string& xmlpath) {/************************************************************************file類data() 返回 char* 的xml文本內(nèi)容size() 返回 unsigned int的文本數(shù)據(jù)長(zhǎng)度定義: rapidxml::file<0> valName("xmlpath");xml_document類parse(Ch *text) 將文本數(shù)據(jù)解析為DOM treeclear() 清空DOM tree定義: rapidxml::xml_document<0> doc;************************************************************************/try{// 用file文件讀入緩沖區(qū)rapidxml::file<> fdoc(xmlpath.c_str());// 打印從文件中讀取的內(nèi)容std::cout << fdoc.data();// 解析獲取DOM實(shí)例rapidxml::xml_document<> doc;doc.parse<0>(fdoc.data());ReadParam(doc);doc.clear();}catch (rapidxml::parse_error e){std::cout << e.what() << std::endl;return;}}static const int buf_len = 2048; static char buf[buf_len] = { 0 }; void readXMLByStream(const std::string& xmlpath) {try{// 清空緩沖區(qū)memset(buf, 0, buf_len);// 判斷文件是否存在if (_access(xmlpath.c_str(), 0) == -1){std::cout << "xml file " << xmlpath << "is not exits!" << std::endl;return;}// 實(shí)例化文件讀取流std::ifstream infile(xmlpath, std::ios::in);if (!infile){std::cout << "file stream instance error!" << std::endl;return;}// 讀取文件內(nèi)容到緩沖區(qū)infile.read(buf, buf_len);// 輸出文件內(nèi)容std::cout << buf << std::endl;// 實(shí)例化DOMrapidxml::xml_document<> doc;doc.parse<0>(buf);ReadParam(doc);doc.clear();infile.close();}catch (rapidxml::parse_error e){std::cout << e.what() << std::endl;return;}}void main() {std::string xmlpath = "ReadExample.xml";readXMLByFile(xmlpath);readXMLByStream(xmlpath);std::system("pause");}

其對(duì)應(yīng)的xml文件如下所示:

<config> <SampleFrequency>25</SampleFrequency><SMOKEFireDetection><Detectors><iter><name>fire</name><version>1.3.0</version><confidence>0.5</confidence><bshowDebugWin>1</bshowDebugWin><usefulLabel>3 4 5</usefulLabel><bStandard>1</bStandard></iter><iter><name>smoke</name><version>1.6.0</version><confidence>0.2</confidence><bshowDebugWin>1</bshowDebugWin><usefulLabel>4</usefulLabel><bStandard>1</bStandard></iter><iter><name>motionanalyze</name><version></version><confidence></confidence><bshowDebugWin>1</bshowDebugWin><usefulLabel></usefulLabel></iter></Detectors><ShowReportWin>1</ShowReportWin> </SMOKEFireDetection></config>

2. 創(chuàng)建XML文件

#include "rapidxml.hpp" #include "rapidxml_utils.hpp" #include "rapidxml_print.hpp" #include <iostream> #include <string> #include <fstream> #include <io.h> #include <stdlib.h>void CreateXml(const std::string& XMLFileName) {//保存Annotation, 把識(shí)別結(jié)果寫(xiě)進(jìn)xml// DOMrapidxml::xml_document<> doc;// node_declaration//rapidxml::xml_node<>* declaration = doc.allocate_node(rapidxml::node_declaration);//declaration->append_attribute(doc.allocate_attribute("version", "1.0"));//declaration->append_attribute(doc.allocate_attribute("encoding", "utf-8"));//doc.append_node(declaration);// node_pirapidxml::xml_node<>* root = doc.allocate_node(rapidxml::node_pi, doc.allocate_string("xml version='1.0' encoding='utf-8'"));doc.append_node(root);// node_commentrapidxml::xml_node<>* comment = doc.allocate_node(rapidxml::node_comment, 0, " 這是對(duì)目標(biāo)的Annotation保存格式 ");doc.append_node(comment);// node_element rapidxml::xml_node<>* node = doc.allocate_node(rapidxml::node_element, "annotation", "information");doc.append_node(node);// node_datarapidxml::xml_node<>* folder = doc.allocate_node(rapidxml::node_element, "folder", "VOC2007");node->append_node(folder);rapidxml::xml_node<>* filename = doc.allocate_node(rapidxml::node_element, "filename", XMLFileName.c_str());node->append_node(filename);// node_element with valuerapidxml::xml_node<>* source = doc.allocate_node(rapidxml::node_element, "source", nullptr);source->append_node(doc.allocate_node(rapidxml::node_element, "database", "My VOC2007 Databas"));source->append_node(doc.allocate_node(rapidxml::node_element, "annotation", "VOC2007"));source->append_node(doc.allocate_node(rapidxml::node_element, "image", "flickr"));source->append_node(doc.allocate_node(rapidxml::node_element, "flickrid", "nullptr"));node->append_node(source);rapidxml::xml_node<>* owner = doc.allocate_node(rapidxml::node_element, "owner", nullptr);owner->append_node(doc.allocate_node(rapidxml::node_element, "flickrid", "nullptr"));owner->append_node(doc.allocate_node(rapidxml::node_element, "name", "aware"));node->append_node(owner);rapidxml::xml_node<>* size = doc.allocate_node(rapidxml::node_element, "size", nullptr);int width = 1920;char cw[128];_itoa_s(width, cw, 128, 10);size->append_node(doc.allocate_node(rapidxml::node_element, "width", cw));int height = 1080;char ch[128];_itoa_s(height, ch, 128, 10);size->append_node(doc.allocate_node(rapidxml::node_element, "height", ch));size->append_node(doc.allocate_node(rapidxml::node_element, "depth", "3"));node->append_node(size);rapidxml::xml_node<>* segmented = doc.allocate_node(rapidxml::node_element, "segmented", "0");node->append_node(segmented);for (int i = 0; i < 2; i++){// node_commentrapidxml::xml_node<>* comment = doc.allocate_node(rapidxml::node_comment, 0, " 目標(biāo)的位置狀態(tài)信息 ");node->append_node(comment);rapidxml::xml_node<>* object = doc.allocate_node(rapidxml::node_element, "object", nullptr);node->append_node(object);object->append_node(doc.allocate_node(rapidxml::node_element, "name", doc.allocate_string("example")));object->append_node(doc.allocate_node(rapidxml::node_element, "pose", "Unspecified"));object->append_node(doc.allocate_node(rapidxml::node_element, "truncated", "0"));object->append_node(doc.allocate_node(rapidxml::node_element, "difficult", "0"));rapidxml::xml_node<>* bndbox = doc.allocate_node(rapidxml::node_element, "bndbox", nullptr);object->append_node(bndbox);bndbox->append_node(doc.allocate_node(rapidxml::node_element, "xmin", "11"));bndbox->append_node(doc.allocate_node(rapidxml::node_element, "ymin", "20"));bndbox->append_node(doc.allocate_node(rapidxml::node_element, "xmax", "1000"));bndbox->append_node(doc.allocate_node(rapidxml::node_element, "ymax", "600"));//打印整個(gè)XML內(nèi)容std::string text;rapidxml::print(std::back_inserter(text), doc, 0);std::cout << text << std::endl;}//打印整個(gè)XML內(nèi)容std::string text;rapidxml::print(std::back_inserter(text), doc, 0);std::cout << text << std::endl;// 輸出DOM到文件std::ofstream outfile(XMLFileName.c_str(), std::ios::out); //ofstream默認(rèn)時(shí),如果文件存在則覆蓋原來(lái)的內(nèi)容,不存在則新建if (outfile){outfile << doc;outfile.close();}doc.clear(); }void main() {const std::string Xmlfilename = "WriteExample.xml";CreateXml(Xmlfilename);std::system("pause");}

其結(jié)果如下圖所示:

<?xml version='1.0' encoding='utf-8' ?> <!--這是對(duì)目標(biāo)的Annotation保存格式--> <annotation><folder>VOC2007</folder><filename>WriteExample.xml</filename><source><database>My VOC2007 Databas</database><annotation>VOC2007</annotation><image>flickr</image><flickrid>nullptr</flickrid></source><owner><flickrid>nullptr</flickrid><name>aware</name></owner><size><width>1920</width><height>1080</height><depth>3</depth></size><segmented>0</segmented><!--目標(biāo)的位置狀態(tài)信息 --><object><name>example</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>11</xmin><ymin>20</ymin><xmax>1000</xmax><ymax>600</ymax></bndbox></object><!--目標(biāo)的位置狀態(tài)信息 --><object><name>example</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>11</xmin><ymin>20</ymin><xmax>1000</xmax><ymax>600</ymax></bndbox></object> </annotation>

3. 修改及增加刪除

首先是一些對(duì)xml的節(jié)點(diǎn)解釋:
xml_node類
1)node_type type() const; 獲取結(jié)點(diǎn)類型 獲取的類型是枚舉的
2)Ch* name() const; 獲取結(jié)點(diǎn)名
3)std::size_t name_size() const; 獲取結(jié)點(diǎn)名長(zhǎng)度
4)Ch* value() const; 獲取結(jié)點(diǎn)值
5)std::size_t value_size() const; 獲取結(jié)點(diǎn)值長(zhǎng)度
6)xml_node* first_node(const Ch *name = 0, std::size_t name_size = 0, bool case_sensitive = true) const; 獲取DOM Tree第一個(gè)子結(jié)點(diǎn)的指針
第一個(gè)參數(shù)為節(jié)點(diǎn)名,如果給定第一個(gè)參數(shù)為”a”, 則該函數(shù)尋找結(jié)點(diǎn)名為a的第一個(gè)子結(jié)點(diǎn);第二個(gè)參數(shù)為結(jié)點(diǎn)名長(zhǎng)度
7)xml_node* last_node(const Ch *name = 0, std::size_t name_size = 0, bool case_sensitive = true) const; 獲取DOM Tree最后一個(gè)子結(jié)點(diǎn)的指針
參數(shù)含義同上
8)xml_attribute* first_attribute(const Ch *name = 0, std::size_t name_size = 0, bool case_sensitive = true) const; 獲取結(jié)點(diǎn)的第一個(gè)屬性指針
9)xml_attribute* next_attribute(const Ch *name = 0, std::size_t name_size = 0, bool case_sensitive = true) const; 獲取結(jié)點(diǎn)的下一個(gè)屬性指針
10)xml_attribute* last_attribute(const Ch *name = 0, std::size_t name_size = 0, bool case_sensitive = true) const; 獲取結(jié)點(diǎn)的最后一個(gè)屬性指針
11)xml_node* previous_sibling(const Ch *name = 0, std::size_t name_size = 0, bool case_sensitive = true) const; 獲取上一個(gè)同級(jí)結(jié)點(diǎn)的指針
12)xml_node* next_sibling(const Ch *name = 0, std::size_t name_size = 0, bool case_sensitive = true) const; 獲取下一個(gè)同級(jí)結(jié)點(diǎn)的指針
13)xml_attribute* first_attribute(const Ch *name = 0, std::size_t name_size = 0, bool case_sensitive = true) const; 獲取第一個(gè)同級(jí)結(jié)點(diǎn)的指針
14)xml_attribute* last_attribute(const Ch *name = 0, std::size_t name_size = 0, bool case_sensitive = true) const; 獲取最后一個(gè)同級(jí)結(jié)點(diǎn)的指針
15)void insert_node(xml_node< Ch > *where, xml_node< Ch > *child); 在第一個(gè)參數(shù)指向的結(jié)點(diǎn)之前,插入一個(gè)結(jié)點(diǎn)
xml_attribute類
1)xml_attribute *previous_attribute(const Ch *name = 0, std::size_t name_size = 0, bool case_sensitive = true) const; 獲取前一個(gè)屬性
2)xml_attribute *next_attribute(const Ch *name = 0, std::size_t name_size = 0, bool case_sensitive = true) const;獲取后一個(gè)屬性

?

#include <iostream> #include <string> #include <fstream> #include "string.h" #include "rapidxml.hpp" #include "rapidxml_print.hpp" #include "rapidxml_utils.hpp"void modifyXML(const char * file_name) {std::string text;//用file解析DOM時(shí)必須是絕對(duì)路徑rapidxml::file<> fdoc(file_name);//打印讀取的文件std::cout<<fdoc.data();rapidxml::xml_document<> doc;doc.parse<0>(fdoc.data());//取得根節(jié)點(diǎn)rapidxml::xml_node<>* root = doc.first_node("config");//刪除第一個(gè)元素if (root->first_node() != nullptr)root->remove_first_node();text = "\r\n移除根節(jié)點(diǎn)下的第一個(gè)元素節(jié)點(diǎn)\r\n";rapidxml::print(std::back_inserter(text), doc, 0); //doc內(nèi)容輸出到text尾處std::cout << text << std::endl;rapidxml::xml_node<>* FunctionNode = root->first_node("SMOKEFireDetection");if (FunctionNode != nullptr)std::cout << "SMOKEFireDetection is not null" << std::endl;elsereturn;//刪除FunctionNode節(jié)點(diǎn)的最后一個(gè)元素if (FunctionNode->last_node() != nullptr)FunctionNode->remove_last_node();text = "\r\n移除根節(jié)點(diǎn)下的FunctionNode的最后一個(gè)元素節(jié)點(diǎn)\r\n";rapidxml::print(std::back_inserter(text), doc, 0);std::cout << text << std::endl;rapidxml::xml_node<char>* DeteNode = FunctionNode->first_node("Detectors");if (DeteNode != nullptr){for (rapidxml::xml_node<char>* node = DeteNode->first_node("iter"); node != nullptr; node = node->next_sibling()){rapidxml::xml_node<>* pName = node->first_node("name");if (pName != nullptr && std::string(pName->value()) == "fire"){//移除根節(jié)點(diǎn)下的FunctionNode結(jié)點(diǎn)下第一個(gè)循環(huán)節(jié)點(diǎn)下的bStandardNode節(jié)點(diǎn)rapidxml::xml_node<> *bStandardNode = node->first_node("bStandard");if (bStandardNode != nullptr){node->remove_node(bStandardNode);}//移除根節(jié)點(diǎn)下的FunctionNode結(jié)點(diǎn)下第一個(gè)循環(huán)節(jié)點(diǎn)下的的usefulLabelNode節(jié)點(diǎn)rapidxml::xml_node<> *usefulLabelNode = node->first_node("usefulLabel");if (usefulLabelNode != nullptr){node->remove_node(usefulLabelNode);}text = "\r\n移除根節(jié)點(diǎn)下的FunctionNode結(jié)點(diǎn)下第一個(gè)循環(huán)節(jié)點(diǎn)下的bStandardNode節(jié)點(diǎn)和usefulLabelNode節(jié)點(diǎn)\r\n";rapidxml::print(std::back_inserter(text), doc, 0);std::cout << text << std::endl;continue;}else if (pName != nullptr && std::string(pName->value()) == "motionanalyze"){//移除根節(jié)點(diǎn)下的FunctionNode結(jié)點(diǎn)下第三個(gè)循環(huán)節(jié)點(diǎn)下的所有節(jié)點(diǎn)DeteNode->remove_node(node);break;}}}text = "\r\n移除根節(jié)點(diǎn)下的FunctionNode結(jié)點(diǎn)下第三個(gè)循環(huán)節(jié)點(diǎn)下的所有節(jié)點(diǎn)\r\n";rapidxml::print(std::back_inserter(text), doc, 0);std::cout << text << std::endl;//在FunctionNode的Detectors節(jié)點(diǎn)處插入一個(gè)KeyBorad節(jié)點(diǎn)rapidxml::xml_node<>* Graphics = FunctionNode->first_node("Detectors");//找到Graphics節(jié)點(diǎn)rapidxml::xml_node<>* new_node = doc.allocate_node(rapidxml::node_element, "KeyBorad", "Logitech");new_node->append_attribute(doc.allocate_attribute("Interface", "USB"));FunctionNode->insert_node(Graphics, new_node);text = "\r\n在FunctionNode的Detectors節(jié)點(diǎn)下面插入一個(gè)KeyBorad節(jié)點(diǎn)\r\n";rapidxml::print(std::back_inserter(text), doc, 0);std::cout << text << std::endl;寫(xiě)入文件std::ofstream out("1.xml");out << doc; }int main() {const char *file_name = "E:\\VS2015Project\\ReadXmlWriteXml\\ReadXmlWriteXml\\RapidExample.xml";modifyXML(file_name);std::system("pause");return 0; }

原始數(shù)據(jù)為:

<config> <SampleFrequency>25</SampleFrequency><SMOKEFireDetection><Detectors><iter><name>fire</name><version>1.3.0</version><confidence>0.5</confidence><bshowDebugWin>1</bshowDebugWin><usefulLabel>3 4 5</usefulLabel><bStandard>1</bStandard></iter><iter><name>smoke</name><version>1.6.0</version><confidence>0.2</confidence><bshowDebugWin>1</bshowDebugWin><usefulLabel>4</usefulLabel><bStandard>1</bStandard></iter><iter><name>motionanalyze</name><version></version><confidence></confidence><bshowDebugWin>1</bshowDebugWin><usefulLabel></usefulLabel></iter></Detectors><ShowReportWin>1</ShowReportWin> </SMOKEFireDetection></config>

修改后為:

<config><SMOKEFireDetection><KeyBorad Interface="USB">Logitech</KeyBorad><Detectors><iter><name>fire</name><version>1.3.0</version><confidence>0.5</confidence><bshowDebugWin>1</bshowDebugWin></iter><iter><name>smoke</name><version>1.6.0</version><confidence>0.2</confidence><bshowDebugWin>1</bshowDebugWin><usefulLabel>4</usefulLabel><bStandard>1</bStandard></iter></Detectors></SMOKEFireDetection> </config>

感謝?https://www.cnblogs.com/MenAngel/p/11552588.html

總結(jié)

以上是生活随笔為你收集整理的rapidxml 文件读写,增加删除节点的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。