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

  1. QDomDocument Object creation and xml Addition of file header
  2. Create a root node
  3. Create child nodes that contain attributes and node values
  4. Create more complex child nodes ( Contains attributes and third level nodes )
  5. 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 :

  1. establish QDomDocument Object and open the file to add its data source
  2. Get root node
  3. Read simpler child nodes ( Only one attribute and node value )
  4. 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
 Insert picture description here

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