Read and write QT XML files

Source: Internet
Author: User

I decided to write down the knowledge I gained every day, and to be able to do it as a basis for flipping. Although the content is very miscellaneous.

SVG is an XML-defined language used to describe two-dimensional vectors and vector/raster graphics.


enum Qiodevice::openmodeflag
Flags Qiodevice::openmode

This enum was used with open () to describe the mode in which a device is opened. It is also returned by OpenMode ().

Constant Value Description
Qiodevice::notopen 0x0000 The device is not open.
Qiodevice::readonly 0x0001 The device is open for reading.
Qiodevice::writeonly 0x0002 The device is open for writing.
Qiodevice::readwrite ReadOnly | WriteOnly The device is open for reading and writing.
Qiodevice::append 0x0004 The device is opened in append mode, so, and all data are written to the end of the file.
Qiodevice::truncate 0x0008 If possible, the device is truncated before it is opened. All earlier contents of the device is lost.
Qiodevice::text 0x0010 When reading, the End-of-line terminators is translated to ' \ n '. When writing, the End-of-line Terminators is translated to the local encoding, for example ' \ r \ n ' for Win32.
Qiodevice::unbuffered 0x0020 Any buffer in the device is bypassed.
Qiodevice::truncate; F covers the original content;

truncated [English] [ˈtrʌŋkeitid] [US] [ˈtrʌŋˌketɪd] new words concise interpretation More information

adj. cut off the top, shortened, truncated

v. (of a truncate past or past participle); truncated; bypassed v. Bypass, neglect (bypass of past participle) of the head or end;


Source: Write XML file
#include "Mainwindow.h"
#include "Ui_mainwindow.h"
#include <iostream>
#include <QTextStream>
#include <QDebug>

Mainwindow::mainwindow (Qwidget *parent):
Qmainwindow (parent),
UI (New Ui::mainwindow)
{
UI-&GT;SETUPUI (this);

QFile *file=new QFile ("/home/qust/qt/xml/1.xml");
if (!file->open (qiodevice::writeonly| qiodevice::readonly))
{
return;
}
Qdomdocument Doc;
Qdomtext text;
qdomelement element;
Qdomprocessinginstruction instruction;
Instruction=doc.createprocessinginstruction ("xml", "Version=\ ' 1.0\ '");
Doc.appendchild (instruction);
Qdomelement root=doc.createelement ("KDevelop");
Doc.appendchild (root);
Qdomelement general=doc.createelement ("General");
Root.appendchild (General);

Element=doc.createelement ("author");
Text=doc.createtextnode ("Zeki");

Element.appendchild (text);
General.appendchild (Element);

Element=doc.createelement ("email");
Text=doc.createtextnode ("caizhiming@tom.com");
Element.appendchild (text);
General.appendchild (Element);

Qtextstream out (file);
Doc.save (out,4);
File->close ();

}

Mainwindow::~mainwindow ()
{
Delete UI;
}


After running:










Read the XML source code:


#include "Mainwindow.h"
#include "Ui_mainwindow.h"
#include <QtXml>
#include <QDebug>
#include <QFile>
Mainwindow::mainwindow (Qwidget *parent):
Qmainwindow (parent),
UI (New Ui::mainwindow)
{
UI-&GT;SETUPUI (this);
Qdomdocument Dom;
QFile *file=new QFile ("/home/qust/qt/xml/1.xml");
if (File->open (qiodevice::readonly))
{
Dom.setcontent (file);

}

Qdomnodelist email=dom.elementsbytagname ("email");
Qdebug () <<email.count ();
Qdebug () <<email.item (0). Toelement (). text ();
Qdomnodelist general=kdevelop.item (0). Toelement (). ChildNodes ();
Qdebug () <<general.item (0). Toelement (). text ();
}

Mainwindow::~mainwindow ()
{
Delete UI;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.