当前位置:网站首页>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
边栏推荐
- 一周精彩内容分享(第13期)
- 【C和指针第14章】预处理器
- Chapter 1 Introduction
- 第1章 引言
- Using huggingface model to translate English
- How to eliminate the character set and sorting rules specially set for fields in MySQL tables?
- Linked list - Sword finger offer interview question 02.07. linked list intersection
- Common shortcuts to VIM editor
- L1-043 reading room
- L1-064 AI core code valued at 100 million
猜你喜欢

What is prescaler in STM32

2 万字详解,吃透 ES!

Recommended SSH cross platform terminal tool tabby

Easy to use example

6k+ star,面向小白的深度学习代码库!一行代码实现所有Attention机制!

Svn server and client installation (Chinese package) and simple use
![[C and pointer Chapter 14] preprocessor](/img/da/a9a15299157389f8738f7c642a9ff7.png)
[C and pointer Chapter 14] preprocessor
![[data mining engineer - written examination] sheen company in 2022](/img/67/c61dfce8c397ab55fab835b6e81a5f.jpg)
[data mining engineer - written examination] sheen company in 2022

容错、熔断的使用与扩展

微信公众号开发:素材管理(临时、永久)
随机推荐
Top and bottom of stack
[C and pointer Chapter 11] dynamic memory allocation
String -- 344. Reverse string
Chapter 0 Introduction and environment configuration
08.01 adjacency matrix
String - 541. Reverse string II
JVM visualvm: multi hop fault handling tool
Use of multithreading in QT
Cgo+gsoap+onvif learning summary: 9. Go and C conduct socket communication and onvif protocol processing
Use and expansion of fault tolerance and fusing
在kuborad图形化界面中,操作Kubernetes 集群,实现mysql中的主从复制
[rust] Why do I suggest you learn rust | a preliminary study of rust
【网络空间安全数学基础第3章】同余
String matching KMP
Microsoft SQL Server database language and function usage (XII)
Judge whether a group of cards can become shunzi (the size of the king is 14,15)
Differences between JS map and foreach
L2-011 play with binary tree
Literature record (part109) -- self representation based unsupervised exemplar selection in a union of subspaces
一周精彩内容分享(第13期)