当前位置:网站首页>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

The header file

Associated files

XML Writing files

(1) establish xml Document format header

(2) Create root node element labels

(3) Create child node element labels

(4) Set tag properties

(5) Set label content

(6) preservation xml file

XML File analysis  

(1) Find all node elements for a specific tagName

(2) QDomNodeList Class introduction

(3)QDomNode Class method

(4) Traversal node

(5) QDomElement And  QDomNode

(6) obtain QDomNode Two methods of attribute value


The header file


qmake: QT += xml

Header: #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

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()

QDomNodeList(const QDomNodeList &n)

~QDomNodeList()

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

Detailed description

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()

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

normalize()

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 " );

原网站

版权声明
本文为[Chenziqing: see]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206242239342559.html