Qt read-write XML file

Source: Internet
Author: User
Tags tagname

XML file


<?xml version= "1.0" encoding= "UTF-8"?>
<Root>
<item attr1= "0" attr2= "1"/>
<itemList>
<item1>0</item1>
<item2>1</item2>
</itemList>
</Root>

QT Code


QtReadWriteXML.h header File

#ifndef Qt_readwritexml_h
#define Qt_readwritexml_h

#include <QtGui>

Class Qt_readwritexml:public Qwidget
{
Q_object

Public
Qt_readwritexml (Qwidget *parent = 0, Qt::wflags flags = 0);
~qt_readwritexml ();
Private Slots:
void ReadXml ();
void WriteXml ();
};

#endif


QtReadWriteXML.cpp Original File

#include "qt_readwritexml.h"
#include <QFile>
#include <QDomDocument>
#include <QXmlStreamWriter>

Qt_readwritexml::qt_readwritexml (Qwidget *parent, Qt::wflags flags): Qwidget (parent, flags)
{
Qvboxlayout *lay=new qvboxlayout ();
SetLayout (lay);

Qpushbutton *b1=new Qpushbutton ("write");
Qpushbutton *b2=new Qpushbutton ("read");
Lay->addwidget (B1);
Lay->addwidget (B2);
Connect (b1,signal (clicked ()), This,slot (WRITEXML ()));
Connect (b2,signal (clicked ()), This,slot (READXML ()));
}

Qt_readwritexml::~qt_readwritexml ()
{

}


Read XML file
void Qt_readwritexml::readxml ()
{
QString filepath= "E:/test.xml";
QFile file (FilePath);
if (File.Open (qfile::readonly| Qfile::text))
{
Qdomdocument Dom;
if (Dom.setcontent (&file))
{
Qdomelement rootdom=dom.documentelement ();
if (rootdom.tagname () = = "Root")
{
Qdomnode Subnode=rootdom.firstchild ();
while (!subnode.isnull ())
{
Qdomelement subelement=subnode.toelement ();
if (subelement.tagname () = = "Item")
{
for (int i=0;i<subelement.attributes (). Count (); i++)
{
Qdebug () <<subelement.attributes (). Item (i). NodeName ();//attr1,attr2
Qdebug () <<subelement.attributes (). Item (i). NodeValue (). ToInt ();//0,1
}
}
else if (subelement.tagname () = = "ItemList")
{
Qdomnode Childnode=subelement.firstchild ();
while (!childnode.isnull ())
{
Qdebug () <<childnode.toelement (). NodeName ();//item1,item2
Qdebug () <<childnode.toelement (). text ();//0,1
Childnode=childnode.nextsibling ();
}
}
Subnode=subnode.nextsibling ();
}
}
}
File.close ();
}
}

Write an XML file
void Qt_readwritexml::writexml ()
{
QString filepath= "E:/test.xml";
QFile file (FilePath);
if (File.Open (qfile::writeonly| Qfile::text))
{
Qxmlstreamwriter XmlWriter (&file);
Xmlwriter.setautoformatting (TRUE);
Xmlwriter.writestartdocument ();
Xmlwriter.writestartelement ("Root");

Xmlwriter.writestartelement ("item");
Xmlwriter.writeattribute ("attr1", "0");
Xmlwriter.writeattribute ("Attr2", "1");
Xmlwriter.writeendelement ();

Xmlwriter.writestartelement ("ItemList");
Xmlwriter.writetextelement ("Item1", "0");
Xmlwriter.writetextelement ("Item2", "1");
Xmlwriter.writeendelement ();

Xmlwriter.writeendelement ();
Xmlwriter.writeenddocument ();
File.close ();
}
}

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.