@[toc]

Introduction to XML Files

XML, commonly referred to as extensible Markup Language (EXTENSIBLE Markup Language), is a markup language used to mark up electronic files to give them structure. Back in 1998, the W3C released the XML1.0 specification to simplify the transfer of document information over the Internet. XML had two pioneers: SGML and HTML, both of which were very successful markup languages, but both had some inherent flaws. XML was born to address their shortcomings.

Simply put, it is to represent the data in a certain format. Much like the JSON format, the following article describes the JSON data format and its parsing:

  • Introduction to JSON Format
  • Parse JSON using the cJSON library
  • Build JSON strings using the cJSON library
  • Parse and build JSON strings using QJson under Qt platform
  • Use of Jansson parsing library in Keil environment — based on STM32F103

XML file Format

XML is a tree structure, usually made up of root nodes + child nodes, or root elements + child elements.

The basic format

The first line of the XML file must be a declaration statement:


      
Copy the code

After the declaration, is the root element. XML must have only one root element with any name:


      
<root>

</root>
Copy the code

XML statement format: element + attribute

< element name attribute name = "attribute value" > </ element name > < Element name attribute name 1= "attribute value 1" Attribute name 2= "Attribute value 2" > </ element name > // When the element does not contain child elements, This can be abbreviated as < element name Attribute name = "attribute value" /> < element name Attribute name 1= "attribute value 1" Attribute name 2= "Attribute value 2" />Copy the code

Example:

// The element contains no child elements<user blog="www.wangchaochao.top" wechat="mcu149"/>
<user csdn_id="whik1194" wechat="mcu149"/>
Copy the code

Or you could write it as

<user blog="www.wangchaochao.top" wechat="mcu149">
</user>
<user csdn_id="whik1194" wechat="mcu149">
</user>
Copy the code

Element contains child elements

<user csdn_id="whik1194">
	<home>https://blog.csdn.net/whik1194</home>
	<wechat>mcu149</wechat>
	<blog>www.wangchaochao.top</blog>
</user>
Copy the code

Example 1: All elements:


      

<root> 
  <csdn id="whik1194"> 
    <home>https://blog.csdn.net/whik1194</home>  
    <follower>709</follower> 
  </csdn>  

  <zhihu id="wangchao149"> 
    <home>https://www.zhihu.com/people/wangchao149</home>  
    <follower>4961</follower> 
  </zhihu> 

</root>
Copy the code

Example 2: Element + attribute


      

<root> 
  <csdn id="whik1194" home="https://blog.csdn.net/whik1194" follower="709"/>  
  <zhihu id="wangchao149" home="https://www.zhihu.com/people/wangchao149" follower="4961"></zhihu> 
</root>
Copy the code

Matters needing attention

  • XML is case sensitive, and the first character cannot be a number or an underscore
  • Tags must come in pairs. If there is a start tag, there must be an end tag, otherwise it is considered a syntax error
  • XML specifies that all attribute values must be enclosed in quotation marks, which can be either single or double. It is recommended to use double quotation marks uniformly

Example of XML parsing in Qt environment

There are three main ways to parse XML in Qt environment:

  • QXmlStreamReader
  • DOM (Document Object Model)
  • SAX (Simple API for XML)

Comparison of advantages and disadvantages:

  • In terms of lines of code, the DOM and QXmlSimpleReader approach has fewer lines of code than the QXmlStreamReader approach.
  • From the point of view of the code logic, the DOM approach is the easiest to understand, the QXmlStreamReader approach is slightly more difficult to understand, and the QXmlSimpleReader approach makes the code difficult to understand because it uses more callbacks and introduces a large number of class data members.
  • In terms of memory footprint, the DOM approach is the most memory-intensive, since you need to build everything into a tree at once, and both DOM and QXmlSimpleReader are less memory intensive.
  • In terms of running time, the DOM is probably a little bit more expensive, because the DOM normally goes through two iterations, one to build the tree and one to build the data that it needs. QXmlSimpleReader and QXmlStreamReader normally only need to traverse once.
  • DOM and QXmlStreamReader should be a little easier in terms of handling exceptions, since no callback functions are involved, but with XML, a lot of times it’s about checking if something is correct, and if it’s wrong, exiting and looking at the XML for errors. Of course, this is also an important term.

For me, DOM is used more often because, for the most part, the XML being parsed is not very large and is mostly only involved in the loading process. If the XML is large or the calls are frequent, consider using the QXmlStreamReader approach.

As for generating XML documents, Qt also provides three ways:

  • QXmlStreamWriter, which corresponds to QXmlStreamReader;
  • DOM, first in memory to generate the DOM tree, and then write the DOM tree to a file. However, unless we maintain a DOM tree in the data structure of the program, it is certainly difficult to temporarily spanning the tree to write;
  • Generating XML documents purely by hand is obviously the most complicated way to do it.

This article only shows you how to parse XML based on the DOM.

First, Qt needs to add XML support by adding a line to the.pro file:

QT += xml
Copy the code

And add the XML file to the resource path, or access it directly through the file name without adding.

Header files include:

#include <QDomDocument>
#include <QDomElement>
Copy the code

Basic operations, file opening:


bool MainWindow::parseXML(QString fileName)
{
	// Open the step1
    if(fileName.isEmpty())
    {
        qDebug() < <"fileName is empty";
        return false;
    }
    QFile file(fileName);
    if(! file.open(QFile::ReadOnly | QFile::Text))
    {
        QMessageBox::warning(this."Warning", fileName + "\n" + file.errorString());
        return false;
    }
    qDebug() < <"file open success: " << fileName;

/ /...
}
Copy the code

It is then loaded into the DOM, and the XML syntax format is validated. If the syntax format is incorrect, the error information is stored in errStr.

	// Step2.xml file to the DOM, and verify the syntax format
    QDomDocument docXML;
    QString errStr;
    int row, column;
    if(! docXML.setContent(&file, false, &errStr, &row, &column))
    {
        QMessageBox::warning(this."Warning", \
            fileName + "\nsetContent failed at line: " + QString::number(row, 10) + "\n"  + errStr);
        file.close(a);return false;
    }
    qDebug() < <"docXML setContent success";
    file.close(a);Copy the code

The above two steps are required for all XML file parsing. Next, parse according to the different XML file formats.

Example 1 Parsing

XML file contentsText content:


      

<root> 
  <csdn id="whik1194"> 
    <home>https://blog.csdn.net/whik1194</home>  
    <follower>709</follower> 
  </csdn>  

  <zhihu id="wangchao149"> 
    <home>https://www.zhihu.com/people/wangchao149</home>  
    <follower>4961</follower> 
  </zhihu> 

</root>
Copy the code

Analytic function:


bool MainWindow::parseXML(QString fileName)
{
    // Open the step1
    // Step2.xml file to the DOM, and verify the syntax format
    / / step3. XML parsing
    QDomElement xmlRoot = docXML.documentElement(a);// Obtain the root node name
    if (xmlRoot.tagName() != "root")
    {
        qDebug() < <"root tagName is not match";
        return false;
    }

    // Subnode list
    QDomNodeList nodeList = xmlRoot.childNodes(a);qDebug() < <"nodeList size: " << nodeList.size(a);//csdn
    for(int i = 0; i < nodeList.size(a); i++) { QDomNode node = nodeList.at(i);
        QDomElement element = node.toElement(a); QString tagName = element.tagName(a);//"csdn" or "zhihu"
        QString id = element.attribute("id");
        qDebug() << tagName << id;
        // Obtain all child nodes: home & follower
        QDomNodeList eleNodeList= element.childNodes(a);for(int j = 0; j < eleNodeList.size(a); j++) { QDomElement ele = eleNodeList.at(j).toElement(a); QString eleTagName = ele.tagName(a);//"home" or "follower"
            //"https://blog.csdn.net/whik1194" or "709"
            //"https://www.zhihu.com/people/wangchao149" or "4961"
            QString eleTagValue = ele.text(a);qDebug() << eleTagName << eleTagValue;
        }
        qDebug() < <"-- -- -- -- -- -- -- -- -- -- -";
    }

    return true;
}
Copy the code

Running result:

file open success:  ":/xml/demo.xml"
docXML setContent success
nodeList size:  2
"csdn" "whik1194"
"home" "https://blog.csdn.net/whik1194"
"follower" "709"
-----------
"zhihu" "wangchao149"
"home" "https://www.zhihu.com/people/wangchao149"
"follower" "4961"
-----------
Copy the code

Example 2 Parsing

XML file contents:Text file contents:


      

<root> 
  <csdn id="whik1194" home="https://blog.csdn.net/whik1194" follower="709"/>  
  <zhihu id="wangchao149" home="https://www.zhihu.com/people/wangchao149" follower="4961"></zhihu> 
</root>
Copy the code

Analytic function:


bool MainWindow::parseXML(QString fileName)
{
    // Open the step1
    // Step2.xml file to the DOM, and verify the syntax format
    / / step3. XML parsing
    QDomElement xmlRoot = docXML.documentElement(a);// Obtain the root node name
    if (xmlRoot.tagName() != "root")
    {
        qDebug() < <"root tagName is not match";
        return false;
    }

    // Subnode list
    QDomNodeList nodeList = xmlRoot.childNodes(a);qDebug() < <"nodeList size: " << nodeList.size(a);//csdn
    for(int i = 0; i < nodeList.size(a); i++) { QDomNode node = nodeList.at(i);
        QDomElement element = node.toElement(a); QString tagName = element.tagName(a);//"csdn" or "zhihu"

        QString id = element.attribute("id");
        QString home = element.attribute("home");
        QString follower = element.attribute("follower");
        qDebug() << tagName << id << home << follower;
        qDebug() < <"-- -- -- -- -- -- -- -- -- -- -";
    }

    return true;
}
Copy the code

Running result:

file open success:  ":/xml/demo.xml"
docXML setContent success
nodeList size:  2
"csdn" "whik1194" "https://blog.csdn.net/whik1194" "709"
"zhihu" "wangchao149" "https://www.zhihu.com/people/wangchao149" "4961"
Copy the code