当前位置: 首页 > wzjs >正文

个人网站建设心得网站信息设计

个人网站建设心得,网站信息设计,安装好的字体怎么用wordpress,手机网站主页推荐TinyXml2库基本介绍 TinyXML2 是 simple、small、efficient 的基于DOM (Document Object Model,文档对象模型) 的开源 C XML文件解析库,可以很方便地应用到现有的项目中 。目前,TinyXML1 开发已经停止,所有…

TinyXml2库基本介绍

        TinyXML2 是 simple、small、efficient 的基于DOM (Document Object Model,文档对象模型) 的开源 C++ XML文件解析库,可以很方便地应用到现有的项目中 。目前,TinyXML1 开发已经停止,所有的开发都转移到了 TinyXML2。TinyXML2 适用于大部分 C/C++ 的项目,经得住考验,是最好的选择。较 TinyXML1 而言,TinyXML2 化繁为简,使用时只需要包含两个文件,而 TinyXML1 需要包含 6 个文件,一般生成静态链接库供项目使用。

        TinyXML2 使用了与 TinyXML1 相似的 API,并且拥有丰富的测试案例。但 TinyXML2 解析器相对 TinyXML1 在代码上是完全重写,它使用更少的内存,效率更高

        TinyXML2 无需 STL,也放弃了对 STL 支持。所有字符串查询均使用 C 风格字符串const char*来表示,省去 string 类型对象的构造,使代码更加简洁。

下载和使用

GitHub - leethomason/tinyxml2: TinyXML2 is a simple, small, efficient, C++ XML parser that can be easily integrated into other programs.

下载后只需要将将tinyxml2.h和tinyxml2.cpp引入项目即可,极为方便。

项目中使用示例

xmlHandle头文件

#pragma once#include "TinyXml2/tinyxml2.h"
#include<string>
#include<map>using namespace::tinyxml2;namespace XmlHandle 
{int CreateXml(const char*xmlpath,const char*rootname);//创建int LoadXml(tinyxml2::XMLDocument&doc, const char*xmlpath);//从路径xmlpath加载int SaveXml(tinyxml2::XMLDocument&doc, const char*xmlpath);//保存xml文件到xmlpath//插入节点 可以携带属性XMLElement* InsertNode(tinyxml2::XMLDocument&doc, XMLElement*ParentRoot, const char*element, const char*value, const std::map<std::string, std::string>&AttributeMap = std::map<std::string, std::string>(), bool HasAttribute = false);//查询节点XMLElement* QueryNodeByName(tinyxml2::XMLDocument&doc, const char*NodeName, const char*Atttibute = nullptr, const char*AttributeValue = nullptr, bool hasAttribute = false);std::string QueryValueByName(XMLElement*node);std::string QueryAttByName(XMLElement*node,const char*attName);//删除节点int DeleteNode(tinyxml2::XMLDocument&doc, XMLElement*ParentRoot, const char*element, const char*Atttibute = nullptr, bool isAttribute = false);//更新节点值或属性XMLElement* UpdateNode(tinyxml2::XMLDocument&doc, XMLElement*Element, const char*element, const char*value, const char*Atttibute = nullptr, const char*AttributeValue = nullptr, bool HasAttribute = false);//序列化std::string DocToString(tinyxml2::XMLDocument&doc);//反序列化bool stringToDoc(const std::string&SmlStr, tinyxml2::XMLDocument&doc);};

xmlHandle的实现文件

#include "XmlHandle.h"int XmlHandle::CreateXml(const char*xmlpath,const char*rootname)
{const char*declartion = "<?xml version = \"1.0\" encoding = \"UTF-8\" ?>";XMLDocument doc;doc.Parse(declartion);//Parse an XML file from a character string. 覆盖XMLElement*root = doc.NewElement(rootname);//新增根节点doc.InsertEndChild(root);//InsertFirstChild InsertAfterChild InsertEndChild root下第一个 次于... 最后一个XMLError ErrorId = doc.SaveFile(xmlpath);return ErrorId;}int XmlHandle::LoadXml(XMLDocument&doc, const char*xmlpath)
{return doc.LoadFile(xmlpath);
}int XmlHandle::SaveXml(tinyxml2::XMLDocument&doc, const char*xmlpath)
{return doc.SaveFile(xmlpath);
}XMLElement* XmlHandle::InsertNode(tinyxml2::XMLDocument&doc, XMLElement*ParentRoot, const char*element, const char*value, const std::map<std::string, std::string>&AttributeMap, bool HasAttribute)
{XMLElement* Node = doc.NewElement(element);if (HasAttribute){for (auto iter : AttributeMap){Node->SetAttribute(iter.first.c_str(), iter.second.c_str());}}if (value != nullptr)//空则表示有子节点{Node->InsertEndChild(doc.NewText(value));}if (ParentRoot){ParentRoot->InsertEndChild(Node);}else{doc.InsertEndChild(Node);}return  Node;
}XMLElement* XmlHandle::QueryNodeByName(XMLDocument&doc, const char*NodeName, const char*Atttibute , const char*AttributeValue, bool hasAttribute)
{XMLElement*root = doc.RootElement();XMLElement* userNode = root->FirstChildElement(NodeName);while (!userNode){if ((hasAttribute&&userNode->Attribute(Atttibute) == AttributeValue) || (!hasAttribute)){break;}userNode = userNode->NextSiblingElement();//下一个兄弟节点}return userNode;
}std::string XmlHandle::QueryValueByName(XMLElement*node)
{if (node){return node->GetText();}return std::string();
}std::string XmlHandle::QueryAttByName(XMLElement*node, const char*attName)
{if (node){return node->Attribute(attName);}return std::string();
}int XmlHandle::DeleteNode(XMLDocument&doc, XMLElement*ParentRoot,const char*element, const char*Atttibute, bool HasAttribute)
{if (ParentRoot){if (element){ParentRoot->DeleteChild(ParentRoot->FirstChildElement(element));}if (HasAttribute){ParentRoot->DeleteAttribute(Atttibute);}return 0;}return -1;
}XMLElement* XmlHandle::UpdateNode(tinyxml2::XMLDocument&doc, XMLElement*Element, const char*element, const char*value, const char*Atttibute, const char*AttributeValue, bool HasAttribute)
{if (Element){Element->SetText(value);if (HasAttribute){Element->SetAttribute(Atttibute, AttributeValue);}return Element;}return nullptr;
}std::string XmlHandle::DocToString(tinyxml2::XMLDocument&doc)
{XMLPrinter printer;doc.Print(&printer);return printer.CStr();
}bool XmlHandle::stringToDoc(const std::string&SmlStr, tinyxml2::XMLDocument&doc)
{XMLError XmlErr = doc.Parse(SmlStr.c_str(), SmlStr.size());return XmlErr == XML_SUCCESS;
}

 xmlHandle的使用举例

void Qxmltest::TestXML()
{//创建using namespace XmlHandle;std::string path = QDir::currentPath().toStdString() + "/Test.xml";int ret = CreateXml(path.c_str(),"XmlDB");//增加节点tinyxml2:: XMLDocument doc;if (0 == LoadXml(doc, path.c_str()))//QueryNodeByName{using AttMap = std::map<std::string, std::string>;AttMap Attritubte;Attritubte.insert(std::make_pair("Name", "ZahngSan"));Attritubte.insert(std::make_pair("Gneder", "Men"));XMLElement*UserNode = InsertNode(doc, doc.RootElement(), "User", nullptr, Attritubte, true);if (UserNode){InsertNode(doc,  UserNode, "hobby", "play games");InsertNode(doc,  UserNode, "Pet", "Dog");InsertNode(doc,  UserNode, "Language", "Chinese");InsertNode(doc,  UserNode, "height", "180");InsertNode(doc,  UserNode, "weight", "80");}Attritubte.clear();Attritubte.insert(std::make_pair("Name", "LiSi"));Attritubte.insert(std::make_pair("Gneder", "Men"));UserNode = InsertNode(doc,  doc.RootElement(), "User", nullptr, Attritubte, true);if (UserNode){InsertNode(doc, UserNode, "hobby", "play ping-pang");InsertNode(doc, UserNode, "Pet", "Cat");InsertNode(doc, UserNode, "Language", "Chinese");InsertNode(doc, UserNode, "height", "181");AttMap weightAtt;weightAtt.insert(std::make_pair("WeightX", "Year"));weightAtt.insert(std::make_pair("WeightY", "WeightValue"));weightAtt.insert(std::make_pair("WeightZ", "UnKnown"));XMLElement*WNode = InsertNode(doc,  UserNode, "weight", nullptr, weightAtt,true);AttMap att2020;att2020.insert(std::make_pair("Year?", "Yes"));att2020.insert(std::make_pair("Weight?", "No"));XMLElement*Node2020 = InsertNode(doc, WNode, "2020", "70", att2020, true);InsertNode(doc,  WNode, "2021", "73");InsertNode(doc,  WNode, "2022", "75");//修改UpdateNode(doc, Node2020, "2020", "69", "Weight", "Yes", true);//删除DeleteNode(doc, WNode, "2022", "WeightZ", true);}//查找QString Display;XMLElement*ZSNode =  QueryNodeByName(doc,"User","name","ZahngSan",true);if (ZSNode){Display+=QString(QueryAttByName(ZSNode, "Gneder").c_str())+'\n';}QString xmlStr =QString::fromStdString(DocToString(doc));Display +=xmlStr;/*tinyxml2::XMLDocument doc2;if (stringToDoc(xmlStr.toStdString(), doc2)){XMLElement*ZSNode = QueryNodeByName(doc, "User", "name", "LiSi", true);if (ZSNode){Display += QString(QueryAttByName(ZSNode, "Gneder").c_str()) + '\n';}}*/ui.textEdit_XML->setText(Display);SaveXml(doc,path.c_str());}
}

http://www.dtcms.com/wzjs/551797.html

相关文章:

  • 水务行业国企门户网站建设wordpress空白页面模板
  • 成都市制作企业网站客户管理软件哪家好
  • 电子书网站开发网站源代码怎么生成网页
  • 郑州做网站哪家专业江山网站制作
  • 网站建设包括内容天猫网站建设目的
  • 象山县建设管理局网站推广引流渠道
  • 长春火车站到吉大一院网站导航栏条源码
  • 圣亚科技网站案例网站模板 北京公司
  • 新网企业邮箱江门关键词优化价格
  • 网站建设应该怎么做卖普洱茶做网站
  • dw网站建设教程视频中企动力公司
  • 南宁自助建站软件露营旅游网站策划书
  • 招聘网站哪个平台比较好网站开发类论文题目
  • 源码超市网站源码郑州住建局官网查询
  • 移动端手机网站制作邯郸新闻
  • 厦门网站设计哪家公司好wordpress更改上传下载目录
  • 做哪种网站赚钱如何做好网络销售技巧
  • 做企业网站用什么cms合肥网络推广公司哪家好
  • 网上购物网站建设规划做外包任务网站
  • 乐清本地网站seo是什么意思广东
  • 数码网站模板天津电商网站建设
  • 科协网站页建设的意义泉州网站建站公司
  • 网页制作相关网站青岛开发区网站建设公司
  • 做网站公司昆山做网站用啥软件好
  • 服务之家网站推广公司示范校建设 成果网站
  • 国外教育网站模板网页制作怎么做模板
  • 个人网站建站系统关键词优化公司费用多少
  • aspnet校友录网站开发wordpress恢复数据库
  • 门户网站开发请示100种晚上禁用的app大全
  • 一个公司设计网站怎么做的黑镜主题2.0wordpress