当前位置:网站首页>Using qdomdocument to manipulate XML files in QT
Using qdomdocument to manipulate XML files in QT
2022-06-25 02:18:00 【Chenziqing: see】
Catalog
(1) establish xml Document format header
(2) Create root node element labels
(3) Create child node element labels
(1) Find all node elements for a specific tagName
(2) QDomNodeList Class introduction
(6) obtain QDomNode Two methods of attribute value
The header file
qmake: QT += xmlHeader: #include<QDomDocument>
QDomDocument --> xml The file pointer
Associated files
QFile file(filePath);
file.open(QIODevice::WriteOnly);
QDomDocument doc;
doc.setContent(&file);
XML Writing files
(1) establish xml Document format header
QDomProcessingInstruction ins;
ins = doc.createProcessingInstruction("xml", "version = \'1.0\' encoding=\'utf-8\'");
(2) Create root node element labels
QDomElement root = doc.createElement("tagname");
doc.appendChild(root);
(3) Create child node element labels
QDomElement dateEmt = doc.createElement("childTagName");
root.appendChild(dateEmt);
(4) Set tag properties
QDomElement dateEmt = doc.createElement("cim:Company");
// Create attribute name
QDomAttr dateAttr = doc.createAttribute(" Property name ");
dateAttr.setNodeValue(" Property content ”);dateEmt.setAttributeNode(dateAttr); // Set tag properties
(5) Set label content
QDomText text = doc.createTextNode("xxx");
dateEmt.appendChild(text);
(6) preservation xml file
QTextStream stream(&file);
doc.save(stream, 4); // Parameters 2 by Labeled tab Indent value
XML File analysis
(1) Find all node elements for a specific tagName
QDomNodeList nodeList = doc.elementsByTagName(" Tag name ")
(2) QDomNodeList Class introduction
QDomNodeList Class is QDomNode List of objects .
Header:
#include <QDomNodeList>
qmake:
QT += xml
- List of all members , Including inherited members
Note: All functions in this class are reentrant.
Public Functions
QDomNodeList(const QDomNodeList &n)
QDomNode
at(int index) const
int
count() const
bool
isEmpty() const
QDomNode
item(int index) const
int
length() const
int
size() const
bool
operator!=(const QDomNodeList &n) const
QDomNodeList &
operator=(const QDomNodeList &n)
bool
operator==(const QDomNodeList &n) const
QDomNodeList Class is QDomNode List of objects .
The list can be through QDomDocument::elementsByTagName() and QDomNode::childNodes() get . Document object model (DOM) These lists are required to be “ Activities of the ”: Whenever you change the underlying document , The contents of the list will be updated .
have access to item() Get a specific node from the list . The number of items in the list is determined by length() return
(3)QDomNode Class method
QDomNode()
QDomNode(const QDomNode &n)
QDomNode
appendChild(const QDomNode &newChild)
QDomNamedNodeMap
attributes() const
QDomNodeList
childNodes() const
void
clear()
QDomNode
cloneNode(bool deep = true) const
int
columnNumber() const
QDomNode
firstChild() const
QDomElement
firstChildElement(const QString &tagName = QString()) const
bool
hasAttributes() const
bool
hasChildNodes() const
QDomNode
insertAfter(const QDomNode &newChild, const QDomNode &refChild)
QDomNode
insertBefore(const QDomNode &newChild, const QDomNode &refChild)
bool
isAttr() const
bool
isCDATASection() const
bool
isCharacterData() const
bool
isComment() const
bool
isDocument() const
bool
isDocumentFragment() const
bool
isDocumentType() const
bool
isElement() const
bool
isEntity() const
bool
isEntityReference() const
bool
isNotation() const
bool
isNull() const
bool
isProcessingInstruction() const
bool
isSupported(const QString &feature, const QString &version) const
bool
isText() const
QDomNode
lastChild() const
QDomElement
lastChildElement(const QString &tagName = QString()) const
int
lineNumber() const
QString
localName() const
QDomNode
namedItem(const QString &name) const
QString
namespaceURI() const
QDomNode
nextSibling() const
QDomElement
nextSiblingElement(const QString &tagName = QString()) const
QString
nodeName() const
NodeType
nodeType() const
QString
nodeValue() const
void
QDomDocument
ownerDocument() const
QDomNode
parentNode() const
QString
prefix() const
QDomNode
previousSibling() const
QDomElement
previousSiblingElement(const QString &tagName = QString()) const
QDomNode
removeChild(const QDomNode &oldChild)
QDomNode
replaceChild(const QDomNode &newChild, const QDomNode &oldChild)
void
save(QTextStream &stream, int indent, EncodingPolicy encodingPolicy = QDomNode::EncodingFromDocument) const
void
setNodeValue(const QString &v)
void
setPrefix(const QString &pre)
QDomAttr
toAttr() const
QDomCDATASection
toCDATASection() const
QDomCharacterData
toCharacterData() const
QDomComment
toComment() const
QDomDocument
toDocument() const
QDomDocumentFragment
toDocumentFragment() const
QDomDocumentType
toDocumentType() const
QDomElement
toElement() const
QDomEntity
toEntity() const
QDomEntityReference
toEntityReference() const
QDomNotation
toNotation() const
QDomProcessingInstruction
toProcessingInstruction() const
QDomText
toText() const
bool
operator!=(const QDomNode &n) const
QDomNode &
operator=(const QDomNode &n)
bool
operator==(const QDomNode &n) const
(4) Traversal node
Use firstChild() Get the first child node of the node ( If there is ), And use nextSibling() Traversal .
(5) QDomElement And QDomNode
I understand it : QDomElement and QDomNode It's all nodes , Difference is that QDomElement Is a separate node ( That is, when there are no sub tags ), have access to tagName() and text() Method to get the name and content of the node . QDomNode have access to toElement() Turn into QDomElement type
(6) obtain QDomNode Two methods of attribute value
QDomNode node;
① QString value = node.attributes().namedItem(" Property name ").nodeValue()
② QString value = QDomElement elem = node.toElement().attribute( " Property name " );
边栏推荐
- Application of TSDB in civil aircraft industry
- Chrysanthemum chain (winter vacation daily question 39)
- jwt
- 一线城市软件测试工资——你拖后腿了吗
- vim的Dirvish中文文档
- Once beego failed to find bee after passing the go get command Exe's pit
- 常用的软件测试工具清单,请查收。
- beescms网站渗透测试和修复意见「建议收藏」
- Integration of metersphere open source continuous testing platform and Alibaba cloud cloud cloud efficient Devops
- js正则匹配数字、大小写字母、下划线、中线和点[通俗易懂]
猜你喜欢

Exploring the mystery of C language program -- C language program compilation and preprocessing

Intranet learning notes (5)

Integration of metersphere open source continuous testing platform and Alibaba cloud cloud cloud efficient Devops

Application of TSDB in civil aircraft industry
![[live review] battle code pioneer phase 7: how third-party application developers contribute to open source](/img/ad/26a302ca724177e37fe123f8b75e4e.png)
[live review] battle code pioneer phase 7: how third-party application developers contribute to open source

做软件安全测试的作用,如何寻找软件安全测试公司出具报告?

罗德与施瓦茨与中关村泛联院合作开展6G技术研究与早期验证

左手梦想 右手责任 广汽本田不光关注销量 还有儿童安全

都2022年了,你还不了解什么是性能测试?

疫情防控,居家办公,网上授课之心得 | 社区征文
随机推荐
【Proteus仿真】Arduino UNO+继电器控制照明设备
Squid 代理服务器之 ACL 访问控制
LINQ 查询(3)
Redis 那些事
Multimodal emotion recognition_ Research on emotion recognition based on multimodal fusion
Dataease template market officially released
获取图片外链的方法–网易相册[通俗易懂]
What are the SQL aggregate functions
把 Oracle 数据库从 Windows 系统迁移到 Linux Oracle Rac 集群环境(2)——将数据库转换为集群模式
How do the TMUX color palette work?
How to quickly familiarize yourself with the code when you join a new company?
当他们在私域里,掌握了分寸感
基本布局-QHBoxLayout类、QVBoxLayout类、QGridLayout类
常用的软件测试工具清单,请查收。
背了八股文,六月赢麻了……
02-Epicor二次开发常用代码
入职一家新公司,如何快速熟悉代码?
Left hand dreams right hand responsibilities GAC Honda not only pays attention to sales but also children's safety
Investigation on key threats of cloud computing applications in 2022
Kaggle 专利匹配比赛赛后总结