神田るみ,一箱战歌之斧,口子窖价格
最近遇到个需要在c++中处理xml文件的需求,虽然对此方面并不是很熟,但好在有github上的项目的帮助,还是收获了足够的相关知识。
常用的或被推荐的xml类库有以下数个选项,不过相较于纯c完成的类库个人还是更倾向于c++的类库:
tinyxml是在寻找更多信息时被多次提及的,因为并不想花费过多时间在做选择题上,于是其似乎成了最终的赢家。
但未曾想它自身还有两个版本。
tinyxml与tinyxml2的相同点:
tinyxml2的优点:
tinyxml的优点:
tinyxml2的第2及第4项优点是我更中意的,所以还是选它吧。
在其github的仓库中下载相关文件,
找到tinyxml2.h与tinyxml2.cpp两个文件,将它们添加至你的工程项目中,这便是所有需要的。
#include <iostream> #include <random> #include "tinyxml2.h" using namespace tinyxml2; void writexmlfile() { xmldocument doc; auto delaration = doc.newdeclaration(); doc.insertfirstchild(delaration); auto root = doc.newelement("root"); doc.insertendchild(root); auto id = doc.newelement("id"); id->settext(666); root->insertendchild(id); auto name = doc.newelement("name"); name->settext("ken"); name->setattribute("blogger", true); root->insertendchild(name); doc.savefile("sample.xml"); } xmldocument* readxmlfile() { auto doc = new xmldocument; doc->loadfile("sample.xml"); auto root = doc->rootelement(); auto id = root->firstchildelement("id"); std::cout << id->gettext() << std::endl; auto name = root->firstchildelement("name"); std::cout << name->gettext() << std::endl; std::cout << name->attribute("blogger") << std::endl; return doc; } int main() { writexmlfile(); auto doc = readxmlfile(); auto root = doc->rootelement(); auto id = root->firstchildelement("id"); doc->deletenode(id); auto randomid = doc->newelement("randomid"); std::default_random_engine e; std::uniform_int_distribution<int> u; auto r = u(e, decltype(u)::param_type(1000000, 9000000)); randomid->settext(r); root->insertfirstchild(randomid); doc->print(); delete doc; }
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
如何在没有core文件的情况下用dmesg+addr2line定位段错误
用QT制作3D点云显示器——QtDataVisualization
网友评论