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

qt使用QDomDocument读写xml文件

在使用QDomDocument读写xml之前需要在工程文件添加:
QT += xml

1.生成xml文件

void createXml(QString xmlName)
{
	QFile file(xmlName);
    if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate |QIODevice::Text))
        return false;
    QDomDocument doc;
    QDomProcessingInstruction instruction; //添加处理命令
    instruction=doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"UTF-8\"");
    doc.appendChild(instruction);
/*
<bookstore>
	<book category="c++">
		<price>98</price>
	</book>
	<book category="语文">
		<price>100</price>
	</book>
</bookstore>
*/
	QDomElement root = doc.createElement("bookstore");
    doc.appendChild(root);

	QDomElement book = doc.createElement("book");
    book.setAttribute("category", "C++");	//生成category节点
    root.appendChild(book);
    QDomElement price= doc.createElement("price");
    book.appendChild(price);
    QDomText text = doc.createTextNode("98");
    price.appendChild(text);
	
	book = doc.createElement("book");
	book.setAttribute("category", QString::fromLocal8Bit("语文"));
    root.appendChild(book);
    price= doc.createElement("price");
    book.appendChild(price);
    text = doc.createTextNode("100");
    price.appendChild(text);
    
	QTextStream stream(&file);
	stream.setCodec("UTF_8");
	doc.save(stream,4,QDomNode::EncodingFromTextStream);
	file.close();
}

在这里插入图片描述
2.读取xml文件

void loadXml(QString xmlName)
{
	QFile file(xmlName);
    if(!file.open(QFile::ReadOnly | QFile::Text))
    {
        return;
    }

    QString strError;
    int errorLine;
    int errorColumn;
    QDomDocument doc;
    if(!doc.setContent(&file, false, &strError, &errorLine, &errorColumn)){
        return;
    }
    QDomElement root = doc.documentElement();
    if(root.tagName() == "bookstore")
    {
		QDomNode book = root.firstChild();
		while(!book.isNull())
		{
			if(book.toElement().tagName() == "book")
			{
				QString str = book.toElement().attribute("category");	//获取category属性内容
				qDebug()<<str;
				QDomNode node = book.firstChild();
				while(!node.isNull())
				{
					if(node.toElement().tagName() == "price")
					{
						QString price = node.toElement().text();
						qDebug()<<price;
					}
					node = node.nextSibling();
				}
			}
			book = book.nextSibling();
		}
	}
}

void appendXml(QDomDocument &doc,QDomElement &root)
{

    QDomElement book = doc.createElement("book");
    book.setAttribute("category", "C++");	//生成category节点
    root.appendChild(book);

    QDomElement price= doc.createElement("price");
    book.appendChild(price);
    QDomText text = doc.createTextNode("98");
    price.appendChild(text);

    book = doc.createElement("book");
    book.setAttribute("category", QString::fromLocal8Bit("语文"));
    root.appendChild(book);

    price= doc.createElement("price");
        text = doc.createTextNode("100");
        price.appendChild(text);
    book.appendChild(price);
}


void openFileWriteXML(QString xmlPath)
{
    QFile file(xmlPath);
    if(!file.open(QFile::ReadOnly | QFile::Text))
    {
        return;
    }

    QString strError;
    int errorLine;
    int errorColumn;
    QDomDocument doc;
    if(!doc.setContent(&file, false, &strError, &errorLine, &errorColumn)){
        return;
    }
    QDomElement root = doc.documentElement();


}

在这里插入图片描述


http://www.kler.cn/news/325974.html

相关文章:

  • 10分钟制作一个简易的word模版
  • 在 React 项目中渲染 Markdown 文件
  • 【深圳大学】大学物理实验2 光栅光谱仪 预习参考
  • 国产RISC-V蓝牙MCU推荐
  • 【mysql相关总结】
  • 个人文章合集 - 前端相关
  • docker 部署 WEB IDE
  • 用 Go 和 Redis 构建一个简单的任务管理系统
  • 【MySQL】服务器管理与配置
  • FPGA学习(1)-mux2,2选1多路器
  • 速盾:网页游戏部署高防服务器有什么优势?
  • 数据结构编程实践20讲(Python版)—02链表
  • CAD图纸加密软件有哪些好用的?10款企业必备的图纸加密软件!
  • javdoc:(JDK9)VISITOR模式遍历语法树(DocCommentTree)获取代码注释中的tag(@return,@param)对象
  • 【Linux】基于驱动框架的程序编写测试
  • 全国糖酒会全域采购商选品会前瞻-见证零售新势力的崛起与变革
  • 第七讲-按钮控件QRadioButton
  • LINUX之Ansible自动化运维工具配置和ssh-keygen配置远程免密钥登录
  • InputStream为什么不能被重复读取?为啥只能被读取一次?
  • 探索 Android DataBinding:实现数据与视图的完美融合
  • 腾讯邮箱上传附件卡、慢、无法上传,下载慢问题处理
  • Harmony 获取定位位置的方式
  • 休眠唤醒不了?你的4G模组不是装睡,而是少了一条指令…
  • Spring Mvc 基础源码分析
  • OceanBase 关于一号表笔记与ERROR 1060(42S21)问题
  • 表驱法优化代码
  • 入职2年的程序员,被劝退了!年纪大了,感觉好绝望!
  • Studying-图论包含的算法总结
  • [Python学习日记-31] Python 中的函数
  • Java开发:文件上传和下载