当前位置:网站首页>QT notes - qtxml
QT notes - qtxml
2022-07-24 12:07:00 【Cool breeze in the old street °】
The header file :#include <QtXml>
understand Xml:
1. The document declaration must be <?xml start , With ?> end ;
2. The document declaration must be from the 0 That's ok 0 Column position starts :
3. Document declaration has only attributes :
a) versioin: Appoint XML Document version . Must attribute , Because we won't choose 1.1, Only select 1.0;
b) encoding: Specifies the encoding of the current document . Optional attribute , The default value is .utf-8:
l Elements element
1. The element is XML The most important part of the document ,
2. The structure of a common element begins with a label 、 Element body 、 The end tag consists of . for example : Hello everyone
3. Element body : An element body can be an element , It can also be text , for example : Hello
4. Empty elements : Empty elements have only start tags , And there's no end tag , But the element must close itself , for example :
5. Element naming :
a) Case sensitive
b) You can't use spaces , You can't use colons :
c) It is not recommended to use XML, xml, Xml start
6. Well formatted XML file , There must be only one root element .
l attribute
1. Attributes are part of an element , It must appear in the start tag of the element
2. Property definition format : Property name = Property value , Where the property value must use single or double reference
3. An element can have O ~ N Attributes , But the attribute with the same name cannot appear in an element
4. Property names cannot use spaces 、 Special characters like colons , And it has to start with a letter

pro Add... To the file QT+=xml
1. open Xml file
2. analysis Xml file
3. Read Xml The contents of the document
increase Xml The contents of the document
Delete Xml The contents of the document
remove Xml The contents of the document
modify Xml The contents of the document
4. Save content to Xml In file
1. open Xml file
QFile file("test.xml");
if (!file.open(QIODevice::ReadOnly))
return;
2. analysis Xml File and determine whether there is any wrong content
QString errorStr;
int errorLine;
int errorCol;
// analysis XML
QDomDocument doc;
if (!doc.setContent(&file,true, &errorStr, &errorLine, &errorCol))
{
qDebug() << QStringLiteral(" Error string :%1, Wrong number of lines :%2, Number of wrong Columns :%3").arg(errorStr).arg(errorLine).arg(errorCol);
file.close();
return;
}
file.close();
3. establish Xml The contents of the document
// Open or create a file
QFile file("test.xml"); // Relative paths 、 Absolute path 、 Resource paths are OK
if (!file.open(QFile::WriteOnly | QFile::Truncate)) //Truncate It means to empty the original content
return;
QDomDocument doc;
// Add processing command
QDomProcessingInstruction instruction;
instruction=doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"UTF-8\"");
doc.appendChild(instruction);
// establish dom The elements of
QDomElement root = doc.createElement("library");
doc.appendChild(root);
// Add a child node under the element
QDomElement book = doc.createElement("book");
// Add the content of the sub node title
// Method 1 ; Create element attributes The values of key value pairs can be various types
book.setAttribute("id", 1);
// Method 2 : Create element attributes The value must be a string
QDomAttr time = doc.createAttribute("time");
time.setValue("2013/6/13");
book.setAttributeNode(time);
QDomElement title = doc.createElement("title");
// Add the content of the element
QDomText text = doc.createTextNode("C++ primer");
title.appendChild(text);
// Add it to your parent node
book.appendChild(title);
root.appendChild(book);
// write file
QTextStream stream(&file);
doc.save(stream, 4);
file.close();
Read XML The contents of the document
// Return root node
QDomElement root = doc.documentElement();
qDebug() << root.nodeName(); // Convert elements to root nodes QString name
// Get the first child node
QDomNode node = root.firstChild();
qDebug() << QStringLiteral(" The first child node element name is :") << node.toElement().tagName();
qDebug() << QStringLiteral(" The node element name of the first child node is :") << node.firstChildElement().nodeName();
// Get the attribute of an element
QDomElement e = node.toElement();
//tagName and nodeName almost
qDebug() << e.tagName() << " " << e.attribute("id") << " "<<e.attribute("time");
// Get all child nodes of a node
QDomNodeList list=e.childNodes();
// The content of an element , Now turn the node into an element , Get content again
qDebug()<<n.toElement().text();
QDomNode node = node.nextSibling(); // Get brother nodes
QDomElement node = node.nextSiblingElement();// Get sibling elements
Delete XML Content
QDomElement root=doc.documentElement();
QDomNodeList list=doc.elementsByTagName("book"); // Find all lists with the same tag name
// Remove the node
root.removeChild(list.at(i));
modify XML Content
QDomNode oldnode=node.firstChild(); // The content between the tags appears as a child node of the node , Now it's Pride and Projudice
node.firstChild().setNodeValue("Emma");
QDomNode newnode=node.firstChild();
node.replaceChild(newnode,oldnode);
Reference blog :
https://www.cnblogs.com/gaowc/p/10700348.html
https://blog.csdn.net/hpu11/article/details/80227093
边栏推荐
- L2-011 玩转二叉树
- Markdown mathematical formula syntax
- 第0章 前言和环境配置
- Record a garbage collection and analysis of gceasy
- MySQL creates partition tables and automatically partitions them by day
- scrapy-redis写项目备忘
- 3、 Implementation principle of MFC message mapping mechanism
- Linked list - 142. Ring linked list II
- Dynamic memory management
- Why is the datetime type field 8 hours earlier when I use flinkcdc to read MySQL's binlog?
猜你喜欢
随机推荐
Three small knowledge points about data product managers
Hash - 18. Sum of four numbers
Linked list - 142. Ring linked list II
Hash - 15. Sum of three numbers
第0章 前言和环境配置
Difference between GCC -l parameter and -l parameter
Script redis write project notes
Common formulas and application scenarios of discrete distribution
[mathematical basis of Cyberspace Security Chapter 9] finite field
08.01 adjacency matrix
Skillfully using command line parameters in Delphi to realize the trigger function of dragging files onto program icons
Leetcode:51. queen n
CCF 1-2 question answering record (2)
leetcode:51. N 皇后
MySql的DDL和DML和DQL的基本语法
Jmeter-While控制器
L2-011 玩转二叉树
Using huggingface model to translate English
Understand the storage and retrieval of data
6k+ star,面向小白的深度学习代码库!一行代码实现所有Attention机制!


![Operational amplifier - Notes on rapid recovery [II] (application)](/img/fd/e12f43e23e6ec76c2b44ce7813e204.png)






