当前位置:网站首页>QT reading and writing XML files (including source code + comments)
QT reading and writing XML files (including source code + comments)
2022-04-23 18:07:00 【Lao Wang who loves cooking】
One 、 Example XML The contents of the document
The following is the... Used in this article xml The content of the document
<?xml version="1.0" encoding="UTF-8"?>
<root>
<childNode1 attruKey="attriVal">childNode1 Val</childNode1>
<childNode2 attruKey="attriVal">
<cChildNode1 shuxing1="1">cChildNode1 Val</cChildNode1>
<cChildNode2 shuxing2="2">cChildNode2 Val</cChildNode2>
</childNode2>
</root>
Two 、XML Writing files
Below is XML File written source code , The code contents are as follows :
- QDomDocument Object creation and xml Addition of file header
- Create a root node
- Create child nodes that contain attributes and node values
- Create more complex child nodes ( Contains attributes and third level nodes )
- write file
// establish QDomDocument object
QDomDocument xDoc;
QDomProcessingInstruction inStruction;
// write in xml The file header (xml Version information and coding information )
inStruction = xDoc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"UTF-8\"");
xDoc.appendChild(inStruction);
// Create a root node and add it to xDoc in
QDomElement root = xDoc.createElement("root");
xDoc.appendChild(root);
// Create child nodes 1
QDomElement childNode1 = xDoc.createElement("childNode1");
// Is a child node childNode1 Set properties and property values
childNode1.setAttribute("attruKey", "attriVal");
//! Is a child node childNode1 Add node value
//! establish QDomText Object and set its value
QDomText nodeVal = xDoc.createTextNode("childNode1 Val");
// Use QDomElement Object's nodes , To add a node value, you need to add QDomText object , Otherwise, the value may not be displayed
childNode1.appendChild(nodeVal);
// Put the child nodes 1 Add to the root node
root.appendChild(childNode1);
//! Create a multi-level node childNode2
// Create multi-level nodes 2
QDomElement childNode2 = xDoc.createElement("childNode2");
// It is a multi-level node childNode2 Set properties and property values
childNode2.setAttribute("attruKey", "attriVal");
// Create child nodes of multi-level nodes 1
QDomElement cChildNode1 = xDoc.createElement("cChildNode1");
// Set up cChildNode1 Properties and values of
cChildNode1.setAttribute("shuxing1", 1);
// Set up cChildNode1 Node value
cChildNode1.appendChild(xDoc.createTextNode("cChildNode1 Val"));
// take cChildNode1 Add nodes to multi-level nodes
childNode2.appendChild(cChildNode1);
// Create child nodes of multi-level nodes 2
QDomElement cChildNode2 = xDoc.createElement("cChildNode2");
// Set up cChildNode2 Properties and values of
cChildNode2.setAttribute("shuxing2", 2);
// Set up cChildNode2 Node value
cChildNode2.appendChild(xDoc.createTextNode("cChildNode2 Val"));
// take cChildNode2 Add nodes to multi-level nodes
childNode2.appendChild(cChildNode2);
// Multi level node childNode2 Add to the root node
root.appendChild(childNode2);
// Appoint xml File path
QFile file("./xmlTest.xml");
// Open as read-only , And will empty the contents of the file
if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
return 0;
// Write to a file using a text stream
QTextStream outputStream(&file);
xDoc.save(outputStream, 4); // Indent four spaces
file.close();
3、 ... and 、XML File reading
3.1 File reading source code
Below is XML File reading source code , The code contents are as follows :
- establish QDomDocument Object and open the file to add its data source
- Get root node
- Read simpler child nodes ( Only one attribute and node value )
- Read more complex child nodes ( Contains attributes and different )
// establish QDomDocument object
QDomDocument xDoc;
// Specifies the number of bytes to read xml File path
QFile file("./xmlTest.xml");
//xml The file is clocked in as read-only
if (!file.open(QIODevice::ReadOnly))
return 0;
// call setContent Function to set the data source
if (!xDoc.setContent(&file)) {
file.close();
return 0;
}
file.close();
// obtain xDoc Medium QDomElement object
QDomElement docElem = xDoc.documentElement();
// obtain docElem The root node
QDomNode node = docElem.firstChild();
//! Get the first child node , And read its properties and values
QDomElement childNode1 = node.toElement();
// obtain childNode1 The attribute value
QString attri1 = childNode1.attribute("attruKey");
// obtain childNode1 Node value ( Since setting the node value requires inserting child nodes , Then you should also read its child nodes )
QDomNode node1Child = childNode1.firstChild();
QString node1Val = node1Child.nodeValue();
// Output attribute value and node value
qDebug() << " Attribute value and node value of the first child node ";
qDebug() << attri1 << node1Val;
// Move the node to the next node
node = node.nextSibling();
//! Get the second node , And use the loop to get each value
QDomElement childNode2 = node.toElement();
qDebug() << " Reading of the second node ";
// obtain childNode1 The attribute value
QString attri2 = childNode2.attribute("attruKey");
qDebug() << " The attribute value of the second node :" << attri2;
// Get the first child node in the second node
QDomNode cChildNode = childNode2.firstChild();
while(!node.isNull()) {
// Get the object of the current child node
QDomElement e = cChildNode.toElement();
if(!e.isNull()) {
qDebug() << e.firstChild().nodeValue(); // the node really is an element.
}
// Get the next node element
cChildNode = cChildNode.nextSibling();
}
3.2 Example of reading results
Below is XML File read output example
summary
XML Files use QDomDocument When writing and reading objects, you need to pay attention to the following , Add when needed QDomText Object settings ; When reading, you need to read its first child node , And get the value .( The more complex the file structure , More complex code is needed to read and write )
Related articles
Qt write in Json file ( Including source code + notes )
Qt read Json file ( Including source code + notes )
Qt Reading and writing ini file ( Including source code + notes )
Friendship tips —— Where can't you understand? It's private , Let's make progress together
( It's not easy to create , Please leave a free praise thank you ^o^/)
notes : This paper summarizes the problems encountered by the author in the process of programming , The content is for reference only , If there are any mistakes, please point out .
notes : If there is any infringement , Please contact the author for deletion
版权声明
本文为[Lao Wang who loves cooking]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231800591059.html
边栏推荐
- Process management command
- 2022江西光伏展,中国分布式光伏展会,南昌太阳能利用展
- [UDS unified diagnostic service] IV. typical diagnostic service (4) - online programming function unit (0x34-0x38)
- Eigen learning summary
- Stanford machine learning course summary
- Robocode tutorial 3 - Robo machine analysis
- positioner
- 纳米技术+AI赋能蛋白质组学|珞米生命科技完成近千万美元融资
- SSD硬盘SATA接口和M.2接口区别(详细)总结
- 2022 Jiangxi Photovoltaic Exhibition, China Distributed Photovoltaic Exhibition, Nanchang Solar Energy Utilization Exhibition
猜你喜欢
PowerDesigner various font settings; Preview font setting; SQL font settings
Robocode tutorial 5 - enemy class
Classification of cifar100 data set based on convolutional neural network
MySQL_01_简单数据检索
Implementation of k8s redis one master multi slave dynamic capacity expansion
MySQL auto start settings start with systemctl start mysqld
re正則錶達式
Deep learning classic network analysis and target detection (I): r-cnn
Jenkspy package installation
Summary of floating point double precision, single precision and half precision knowledge
随机推荐
How to ensure the security of futures accounts online?
Process management command
Clion installation tutorial
idea中安装YapiUpload 插件将api接口上传到yapi文档上
Nat commun | current progress and open challenges of applied deep learning in Bioscience
NVIDIA Jetson: GStreamer and openmax (GST OMX) plug-ins
Go's gin framework learning
Vulnérabilité d'exécution de la commande de fond du panneau de commande JD - freefuck
Romance in C language
读取excel,int 数字时间转时间
JD-FreeFuck 京東薅羊毛控制面板 後臺命令執行漏洞
Crawler for querying nicknames and avatars based on qqwebapi
C language to achieve 2048 small game direction merging logic
mysql自动启动设置用Systemctl start mysqld启动
Docker 安裝 Redis
Fashion classification case based on keras
undefined reference to `Nabo::NearestNeighbourSearch
Robocode tutorial 5 - enemy class
MySQL_ 01_ Simple data retrieval
re正則錶達式