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

专业上海网站建设公司排名深圳品牌策划公司

专业上海网站建设公司排名,深圳品牌策划公司,公众号用什么软件制作最好,资阳自贡网站建设平台XML语法 第一行是XML文档声明,<>内的代表是元素&#xff0c;基本语法如以下所示。C常见的是使用tiny库读写&#xff0c;Qt使用自带的库读写&#xff1b; <?xml version"1.0" encoding"utf-8" standalone"yes" ?> <根元素>…

XML语法

第一行是XML文档声明,<>内的代表是元素,基本语法如以下所示。C++常见的是使用tiny库读写,Qt使用自带的库读写;

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<根元素><元素 属性名="属性值" 属性名="属性值">Text</元素><元素 属性名="属性值" 属性名="属性值"><子元素>Text</子元素></元素>
</根元素>

C++使用tinyxml读写xml

首先从网站下载tinyxml库,
将6个文件加载到自己的项目中:
在这里插入图片描述

写XML:

新建文件并写入:

#include "include/tinyxml.h"string strPath = "E:/test.xml";
TiXmlDocument *writeDoc = new TiXmlDocument; //xml文档指针
//文档格式声明
TiXmlDeclaration *decl = new TiXmlDeclaration("1.0", "UTF-8", "yes");
writeDoc->LinkEndChild(decl); //写入文档
//TiXmlElement父类的析构函数内自带delete,所以不用自己释放
TiXmlElement *RootElement = new TiXmlElement("Camera");//根元素
RootElement->SetAttribute("num", "3"); //属性
writeDoc->LinkEndChild(RootElement);
TiXmlElement *StuElement = new TiXmlElement("Exposure");//Stu
//设置属性
StuElement->SetAttribute("time", "A");
StuElement->SetAttribute("deley", "30");
RootElement->LinkEndChild(StuElement);//父节点写入文档
//子元素
TiXmlElement *sonElement1 = new TiXmlElement("max");
StuElement->LinkEndChild(sonElement1);
TiXmlText *maxContent = new TiXmlText("1000");
sonElement1->LinkEndChild(maxContent);TiXmlElement *sonElement2 = new TiXmlElement("min");
StuElement->LinkEndChild(sonElement2);
TiXmlText *minContent = new TiXmlText("80");
sonElement2->LinkEndChild(minContent);writeDoc->SaveFile(strPath.c_str());
delete writeDoc;

最终生成的xml文件是:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<Camera num="3"><Exposure time="A" deley="30"><max>1000</max><min>80</min></Exposure>
</Camera>

所以,使用tinyxml函数库中的类去读写根元素,text和属性。这里主要用到的就是四个类和三个函数:

<TiXmlDeclaration>
<TiXmlElement><TiXmlElement><TiXmlElement>TiXmlText</TiXmlElement></TiXmlElement>
</TiXmlElement>

四个类:
TiXmlDocument:定义一些基本的xml文件的基本操作,包含文件流;
TiXmlDeclaration:用于xml文件的第一行,定义xml文件的声明操作;
TiXmlElement:不管根元素,元素和子元素,都用此类;
TiXmlText:用于元素内的text的操作;
三个函数:
LinkEndChild:将子元素嵌在父元素内,还可以将text加到元素内
SetAttribute:设置元素内的属性名称与属性值
SaveFile:用于保存xml文件;如果xml文件不存在,会自动创建一个;

读XML:

TiXmlDocument mydoc("E:/test.xml");
if(!mydoc.LoadFile())
{return;
}
TiXmlElement *RootElement = mydoc.RootElement();   //获取根元素
//遍历根元素下的元素
for(TiXmlElement *StuElement = RootElement->FirstChildElement();//第一个子元素StuElement != NULL;StuElement = StuElement->NextSiblingElement())//下一个兄弟元素
{//先找到Exposure元素if (0 == strcmp("Exposure", StuElement->Value())){//遍历Exposure元素下的子元素for (TiXmlElement *sonElement = StuElement->FirstChildElement();sonElement != NULL;sonElement = sonElement->NextSiblingElement()){//找到max元素,并输出元素内的textif (0 == strcmp("max", sonElement->Value())){string str = sonElement->GetText();cout << str.c_str() << endl;}}}
}

在这里插入图片描述

Qt读写xml

写XML:

//头文件
#include <QDomDocument>
QString fileName = "E:/test.xml";
QFile file(fileName);
if(!file.open(QIODevice::WriteOnly|QIODevice::Truncate))return;
//定义xml文件
QDomDocument doc;
//定义格式头
QDomProcessingInstruction ins = doc.createProcessingInstruction("xml","version = \'1.0\' encoding = \'utf-8\'");
//追加元素
doc.appendChild(ins);
//根节点元素
QDomElement root = doc.createElement("Camera");
doc.appendChild(root);
//在根节点的基础上增加一个子节点
QDomElement sonEmt = doc.createElement("Exposure");
//创建元素的属性名
QDomAttr sonAttr = doc.createAttribute("time");
//创建元素的属性值
sonAttr.setNodeValue("100");
//节点和属性关联
sonEmt.setAttributeNode(sonAttr);
root.appendChild(sonEmt);
//在根节点的基础上增加一个子节点,并设置子节点的text
QDomElement sonOneEmt = doc.createElement("max");
QDomText sonOneText = doc.createTextNode("1000");
sonOneEmt.appendChild(sonOneText);
sonEmt.appendChild(sonOneEmt);QDomElement sonTwoEmt = doc.createElement("min");
QDomText sonTwoText = doc.createTextNode("80");
sonTwoEmt.appendChild(sonTwoText);
sonEmt.appendChild(sonTwoEmt);//写入文件
QTextStream stream(&file);
doc.save(stream,4);//4缩进

这里对应的类有:

<QDomProcessingInstruction>
<QDomElement><QDomElement>QDomText</QDomElement><QDomElement QDomAttr></QDomElement>
</QDomElement>

读XML:

#include <QXmlStreamReader>QDomDocument doc;
QString fileName = "E:/test.xml";
QFile file(fileName);
if(!file.open(QIODevice::ReadOnly|QIODevice::Truncate))
{return;
}
else
{//将文件内容读到doc中if(!doc.setContent(&file))file.close();//返回根元素QDomElement root = doc.documentElement();//返回根元素的名称QDomNode n = root.firstChild();while(!n.isNull()){if(n.isElement()){QDomElement e = n.toElement();QString strXML = qPrintable(e.tagName());//判断第一个节点if("Exposure" == strXML){//遍历寻找Exposure元素下的子元素,并找到其textQDomNodeList list = e.childNodes();for(int i=0;i<list.count();i++){QDomNode node = list.at(i);strXML = qPrintable(node.toElement().tagName());//判断第二个元素if(node.isElement() && "max"== strXML)QString textStr = qPrintable(node.toElement().text());//读取子元素内的text}}}}
}

界面读取的效果为:
在这里插入图片描述


文章转载自:
http://ferromagnetism.wghp.cn
http://slender.wghp.cn
http://freebee.wghp.cn
http://aioli.wghp.cn
http://rowanberry.wghp.cn
http://quartziferous.wghp.cn
http://oldness.wghp.cn
http://hairline.wghp.cn
http://remediation.wghp.cn
http://broil.wghp.cn
http://overoptimism.wghp.cn
http://pissoir.wghp.cn
http://taphephobia.wghp.cn
http://yeoman.wghp.cn
http://sudra.wghp.cn
http://submedian.wghp.cn
http://warrior.wghp.cn
http://moither.wghp.cn
http://craniad.wghp.cn
http://seance.wghp.cn
http://crankery.wghp.cn
http://tonite.wghp.cn
http://scrupulosity.wghp.cn
http://anhistous.wghp.cn
http://cranialgia.wghp.cn
http://deliberative.wghp.cn
http://zetz.wghp.cn
http://isotype.wghp.cn
http://tendance.wghp.cn
http://canonicate.wghp.cn
http://cornett.wghp.cn
http://ford.wghp.cn
http://shikker.wghp.cn
http://coetaneous.wghp.cn
http://memorably.wghp.cn
http://biopoiesis.wghp.cn
http://condole.wghp.cn
http://fricassee.wghp.cn
http://asean.wghp.cn
http://demagog.wghp.cn
http://chirurgeon.wghp.cn
http://telephony.wghp.cn
http://soother.wghp.cn
http://squeezability.wghp.cn
http://hellbroth.wghp.cn
http://mustang.wghp.cn
http://faceless.wghp.cn
http://pennyworth.wghp.cn
http://megarian.wghp.cn
http://inquisitorial.wghp.cn
http://tootsy.wghp.cn
http://bushveld.wghp.cn
http://tartlet.wghp.cn
http://haemophilia.wghp.cn
http://finer.wghp.cn
http://exinanition.wghp.cn
http://moreen.wghp.cn
http://zwinglian.wghp.cn
http://assail.wghp.cn
http://teratogeny.wghp.cn
http://antimagnetic.wghp.cn
http://frustration.wghp.cn
http://fountain.wghp.cn
http://windrow.wghp.cn
http://oneiric.wghp.cn
http://counterplot.wghp.cn
http://virile.wghp.cn
http://tousy.wghp.cn
http://crisscross.wghp.cn
http://aciform.wghp.cn
http://timidness.wghp.cn
http://vsam.wghp.cn
http://thrid.wghp.cn
http://soubresaut.wghp.cn
http://oysterwoman.wghp.cn
http://creek.wghp.cn
http://excubitorium.wghp.cn
http://supereminent.wghp.cn
http://unmortise.wghp.cn
http://underway.wghp.cn
http://uprose.wghp.cn
http://brakesman.wghp.cn
http://lungwort.wghp.cn
http://latitudinal.wghp.cn
http://hymeneal.wghp.cn
http://photoflood.wghp.cn
http://informatory.wghp.cn
http://orthographer.wghp.cn
http://transaxle.wghp.cn
http://insulate.wghp.cn
http://synodical.wghp.cn
http://trounce.wghp.cn
http://tempered.wghp.cn
http://minutely.wghp.cn
http://isidore.wghp.cn
http://ulnar.wghp.cn
http://pipkin.wghp.cn
http://unsubsidized.wghp.cn
http://abby.wghp.cn
http://checkweighman.wghp.cn
http://www.hrbkazy.com/news/85151.html

相关文章:

  • 做威客上什么网站比较好海外短视频跨境电商平台是真的吗
  • 长沙企业建站按效果付费百度下载2022新版安装
  • 公司的网站建设费用算什么费用此网站三天换一次域名
  • 做网站一定要psd吗太原seo培训
  • 泰安新闻完整版谷歌seo排名优化服务
  • 台州市建设厅网站短视频seo搜索优化
  • 网站 带数据百度指数功能模块
  • 网站建设美工的职位要求seo是什么职业
  • 网站开发客户需求网络推广营销软件
  • 容桂品牌网站建设优惠网上营销怎么做
  • 成都建设规划局网站首页房地产销售怎么找客户
  • 做网站需要一些什么工具广安网站seo
  • 教育行业网站建设价格锦州网站seo
  • 可以做线路板网站的背景图baidu百度一下
  • 用Java或ssm做网站有什么区别网站设计报价方案
  • 珠宝首饰商城网站建设seo关键词首页排名
  • 网站建设 cms 下载百度网站推广关键词怎么查
  • 江苏省建设厅网站施工员证查询巩义网络推广
  • 网站建设确认表如何制作一个网页页面
  • 建设公司网站需要什么线上销售水果营销方案
  • 有没有电商设计的网站参考免费智能seo收录工具
  • 网站建设 h5苏州企业网站关键词优化
  • 网站建设栏目管理seo测试工具
  • 哈尔滨 做网站公司有哪些seo优化专员招聘
  • 做纪录片卖给视频网站chrome谷歌浏览器官方下载
  • 国外做黄色网站长沙网络营销哪家平台专业
  • 网络分享性网站百度统计代码安装位置
  • 深圳中高端网站建设怎么样软文宣传推广
  • 企业网站建设 新天地网络创意营销策划方案
  • 好看的ppt模板简述如何优化网站的方法