Qt5 Tutorial QtXML - 2020
In this tutorial, we will learn about QtXML.
To include the definitions of the module's classes, use the following directive:
#include <QtXml>
To link against the module, add this line to our qmake .pro file:
QT += xml
Though the QtXML docuemnt says:
The module is not actively maintained anymore.
Please use the QXmlStreamReader and QXmlStreamWriter classes in Qt Core instead.
We still need to understand QtXML to move on to the other topics.
In this example, we will write a XML document.
#include <QtCore> #include <QtXML> #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); // Create a document to write XML QDomDocument document; // Making the root element QDomElement root = document.createElement("Dorms"); // Adding the root element to the docuemnt document.appendChild(root); // Writing to a file QFile file("C:/Test/myXLM.xml"); if(!file.open(QIODevice::WriteOnly | QIODevice::Text)) { qDebug() << "Open the file for writing failed"; } else { QTextStream stream(&file;); stream << document.toString(); file.close(); qDebug() << "Writing is done"; } return a.exec(); }
First, we need to create a document:
QDomDocument document;
Then, make a root element and attach it to the document:
// Making the root element QDomElement root = document.createElement("Dorms"); // Adding the root element to the docuemnt document.appendChild(root);
Then, open a file and start to writng using QTextStream:
QTextStream stream(&file;); stream << document.toString();
We can open the XML document to see using XML editor:
<?xml version="1.0"?> <Dorms/>
If we add child elements:
// Adding the root element to the docuemnt document.appendChild(root); // Adding more elements for(int i = 0; i < 5; i++) { QDomElement dorm = document.createElement("Dorm"); dorm.setAttribute("Name", "Dorm Building " + QString::number(i)); dorm.setAttribute("ID", QString::number(i)); root.appendChild(dorm); }
Open the XML doc:
<Dorms> <Dorm ID="0" Name="Dorm Building 0"/> <Dorm ID="1" Name="Dorm Building 1"/> <Dorm ID="2" Name="Dorm Building 2"/> <Dorm ID="3" Name="Dorm Building 3"/> <Dorm ID="4" Name="Dorm Building 4"/> </Dorms>
Now, we want to put rooms to each dorm building:
document.appendChild(root); // Adding more elements for(int i = 0; i < 5; i++) { QDomElement dorm = document.createElement("Dorm"); dorm.setAttribute("Name", "Dorm Building " + QString::number(i)); dorm.setAttribute("ID", QString::number(i)); root.appendChild(dorm); // Adding rooms to each dorm building for(int j = 0; j < 3; j++) { QDomElement room = document.createElement("Room"); room.setAttribute("Name", "Room " + QString::number(j)); room.setAttribute("ID", QString::number(j)); dorm.appendChild(room); } }
Indeed, each Dorm building has rooms now:
<Dorms> <Dorm ID="0" Name="Dorm Building 0"> <Room ID="0" Name="Room 0"/> <Room ID="1" Name="Room 1"/> <Room ID="2" Name="Room 2"/> </Dorm> <Dorm ID="1" Name="Dorm Building 1"> <Room ID="0" Name="Room 0"/> <Room ID="1" Name="Room 1"/> <Room ID="2" Name="Room 2"/> </Dorm> <Dorm ID="2" Name="Dorm Building 2"> <Room ID="0" Name="Room 0"/> <Room ID="1" Name="Room 1"/> <Room ID="2" Name="Room 2"/> </Dorm> <Dorm ID="3" Name="Dorm Building 3"> <Room ID="0" Name="Room 0"/> <Room ID="1" Name="Room 1"/> <Room ID="2" Name="Room 2"/> </Dorm> <Dorm ID="4" Name="Dorm Building 4"> <Room ID="0" Name="Room 0"/> <Room ID="1" Name="Room 1"/> <Room ID="2" Name="Room 2"/> </Dorm> </Dorms>
Here is the final version of source code, main.cpp.
or#include <QtCore> #include <QtXML> #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); // Create a document to write XML QDomDocument document; // Making the root element QDomElement root = document.createElement("Dorms"); // Adding the root element to the docuemnt document.appendChild(root); // Adding more elements for(int i = 0; i < 5; i++) { QDomElement dorm = document.createElement("Dorm"); dorm.setAttribute("Name", "Dorm Building " + QString::number(i)); dorm.setAttribute("ID", QString::number(i)); root.appendChild(dorm); // Adding rooms to each dorm building for(int j = 0; j < 3; j++) { QDomElement room = document.createElement("Room"); room.setAttribute("Name", "Room " + QString::number(j)); room.setAttribute("ID", QString::number(j)); dorm.appendChild(room); } } // Writing to a file QFile file("C:/Test/myXLM.xml"); if(!file.open(QIODevice::WriteOnly | QIODevice::Text)) { qDebug() << "Open the file for writing failed"; } else { QTextStream stream(&file;); stream << document.toString(); file.close(); qDebug() << "Writing is done"; } return a.exec(); }
Qt 5 Tutorial
- Hello World
- Signals and Slots
- Q_OBJECT Macro
- MainWindow and Action
- MainWindow and ImageViewer using Designer A
- MainWindow and ImageViewer using Designer B
- Layouts
- Layouts without Designer
- Grid Layouts
- Splitter
- QDir
- QFile (Basic)
- Resource Files (.qrc)
- QComboBox
- QListWidget
- QTreeWidget
- QAction and Icon Resources
- QStatusBar
- QMessageBox
- QTimer
- QList
- QListIterator
- QMutableListIterator
- QLinkedList
- QMap
- QHash
- QStringList
- QTextStream
- QMimeType and QMimeDatabase
- QFile (Serialization I)
- QFile (Serialization II - Class)
- Tool Tips in HTML Style and with Resource Images
- QPainter
- QBrush and QRect
- QPainterPath and QPolygon
- QPen and Cap Style
- QBrush and QGradient
- QPainter and Transformations
- QGraphicsView and QGraphicsScene
- Customizing Items by inheriting QGraphicsItem
- QGraphicsView Animation
- FFmpeg Converter using QProcess
- QProgress Dialog - Modal and Modeless
- QVariant and QMetaType
- QtXML - Writing to a file
- QtXML - QtXML DOM Reading
- QThreads - Introduction
- QThreads - Creating Threads
- Creating QThreads using QtConcurrent
- QThreads - Priority
- QThreads - QMutex
- QThreads - GuiThread
- QtConcurrent QProgressDialog with QFutureWatcher
- QSemaphores - Producer and Consumer
- QThreads - wait()
- MVC - ModelView with QListView and QStringListModel
- MVC - ModelView with QTreeView and QDirModel
- MVC - ModelView with QTreeView and QFileSystemModel
- MVC - ModelView with QTableView and QItemDelegate
- QHttp - Downloading Files
- QNetworkAccessManager and QNetworkRequest - Downloading Files
- Qt's Network Download Example - Reconstructed
- QNetworkAccessManager - Downloading Files with UI and QProgressDialog
- QUdpSocket
- QTcpSocket
- QTcpSocket with Signals and Slots
- QTcpServer - Client and Server
- QTcpServer - Loopback Dialog
- QTcpServer - Client and Server using MultiThreading
- QTcpServer - Client and Server using QThreadPool
- Asynchronous QTcpServer - Client and Server using QThreadPool
- Qt Quick2 QML Animation - A
- Qt Quick2 QML Animation - B
- Short note on Ubuntu Install
- OpenGL with QT5
- Qt5 Webkit : Web Browser with QtCreator using QWebView Part A
- Qt5 Webkit : Web Browser with QtCreator using QWebView Part B
- Video Player with HTML5 QWebView and FFmpeg Converter
- Qt5 Add-in and Visual Studio 2012
- Qt5.3 Installation on Ubuntu 14.04
- Qt5.5 Installation on Ubuntu 14.04
- Short note on deploying to Windows
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization