当前位置:网站首页>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
边栏推荐
- Queue solving Joseph problem
- Summary of floating point double precision, single precision and half precision knowledge
- Reptile efficiency improvement method
- 7-21 wrong questions involve knowledge points.
- re正则表达式
- RC smart pointer in rust
- Excel opens large CSV format data
- word frequency count
- Crawler for querying nicknames and avatars based on qqwebapi
- What are the relationships and differences between threads and processes
猜你喜欢
Qtablewidget usage explanation
[UDS unified diagnostic service] IV. typical diagnostic service (6) - input / output control unit (0x2F)
Jenkspy package installation
Re expression régulière
Deep learning classic network analysis and target detection (I): r-cnn
cv_ Solution of mismatch between bridge and opencv
ArcGIS license error -15 solution
C network related operations
Map basemap Library
A few lines of code teach you to crawl lol skin pictures
随机推荐
Arcpy adds fields and loop assignments to vector data
re正則錶達式
C#字节数组(byte[])和字符串相互转换
Rust: how to implement a thread pool?
RC smart pointer in rust
Summary of floating point double precision, single precision and half precision knowledge
Array rotation
Flash - Middleware
Qt读写XML文件(含源码+注释)
线上怎么确定期货账户安全的?
C language array processing batch data
Docker installation MySQL
Nat commun | current progress and open challenges of applied deep learning in Bioscience
cv_ Solution of mismatch between bridge and opencv
Classes and objects
Map basemap Library
_ FindText error
From source code to executable file
Cells in rust share variable pointers
Crawler for querying nicknames and avatars based on qqwebapi