QT read-write XML file

Source: Internet
Author: User
Tags tagname

1. Dom Mode

Write the XML file, write it in qdomtext form, and the code is as follows

QFile file ("D:/test.txt");
File.Open (qiodevice::writeonly);

Qdomdocument Doc;
Qdomprocessinginstruction instruction;

instruction = doc.createprocessinginstruction ("xml", "Version = \ ' 1.0\ '");
Doc.appendchild (instruction);
Qdomelement root = doc.createelement ("html");
Doc.appendchild (root);

Qdomelement data;
Qdomelement tmp;
Qdomtext text;

data = Doc.createelement ("title");
Text = Doc.createtextnode (Qstringliteral ("See more"));
Data.appendchild (text);
Root.appendchild (data);

TMP = Doc.createelement ("book");
Text = Doc.createtextnode (Qstringliteral ("novel"));
Tmp.appendchild (text);
Root.appendchild (TMP);


Qtextstream out (&file);
Doc.save (out, 4);

File.close ();

Read the code for

QFile file ("D:/test.txt");
File.Open (qiodevice::readonly);
Qdomdocument Doc;
Doc.setcontent (&file);
Qdomelement root = Doc.documentelement ();
for (Qdomnode node = Root.firstchild ();!node.isnull (); node = node.nextsibling ()) {
	qdomelement element = Node.toelem ENT ();
	QString tagName = Element.tagname ();
	if (TagName = = "title") {
		qdebug () << element.text ();
	} else if (TagName = = "book") {
		qdebug () << Element.text ();
	}
}
File.close ();


Write in qdomattr way:

QFile file ("D:/test.txt");
File.Open (qiodevice::writeonly);

Qdomdocument Doc;
Qdomprocessinginstruction instruction;

instruction = doc.createprocessinginstruction ("xml", "Version = \ ' 1.0\ '");
Doc.appendchild (instruction);
Qdomelement root = doc.createelement ("html");
Doc.appendchild (root);

Qdomattr data;
QDOMATTR tmp;

data = Doc.createattribute ("title");
Data.setvalue (Qstringliteral ("See more"));
Root.setattributenode (data);

TMP = Doc.createattribute ("book");
Tmp.setvalue (Qstringliteral ("novel"));
Root.setattributenode (TMP);


Qtextstream out (&file);
Doc.save (out, 4);

File.close ();

Read Qdomattr

QFile file ("D:/test.txt");
File.Open (qiodevice::readonly);
Qdomdocument Doc;
Doc.setcontent (&file);
Qdomelement root = Doc.documentelement ();

Qdebug () << Root.attribute ("title");
Qdebug () << root.attribute ("book");

File.close ();

2, with Qxmlstreamreader and Qxmlstreamwriter

Write files, set up associated files, start with WriteStartDocument, end with WriteEndDocument

Use WriteStartElement and writeendelement to represent the beginning and end of an element, which need to be used in pairs

<name>a</name> form, with Writetextelement

<name a= ""/> form, with WriteAttribute

QFile file ("Test.txt");
File.Open (qiodevice::writeonly);
Qxmlstreamwriter Xmlstreamwriter (&file);
Xmlstreamwriter.setautoformatting (TRUE);
Xmlstreamwriter.writestartdocument ();
Xmlstreamwriter.writestartelement ("xml");
Xmlstreamwriter.writestartelement ("Round");
Xmlstreamwriter.writeattribute ("id", mroundid);
Xmlstreamwriter.writeattribute ("TableID", Mtableid);
Player1 xmlstreamwriter.writestartelement ("Player1");
Xmlstreamwriter.writeattribute ("Framescore", mplayer1frame);
Xmlstreamwriter.writeattribute ("Points", mplayer1point);
  Xmlstreamwriter.writeattribute ("Break", mplayer1continuepoint);   Xmlstreamwriter.writecharacters ("Hello World");
Add content xmlstreamwriter.writeendelement ();
Player2 xmlstreamwriter.writestartelement ("Player2");
Xmlstreamwriter.writeattribute ("Framescore", mplayer2frame);
Xmlstreamwriter.writeattribute ("Points", mplayer2point);
Xmlstreamwriter.writeattribute ("Break", mplayer2continuepoint);
Xmlstreamwriter.writeendelement (); Xmlstreamwriter.writeendelement ();
Xmlstreamwriter.writeendelement (); Xmlstreamwriter.writeenddocument (); File.close ();
The results are as follows:

<?xml version= "1.0" encoding= "UTF-8"?>
<xml>
    <round id= "" tableid= "1" >
        <player1 Framescore= "points=" "break=" ">hello world</player1>
        <player2 framescore=" "points=" "break=" "/ >
    </round>
</xml>

When reading a file, use Isstartelement to determine the beginning of the sign.

<name a= "" ></name> form, with attributes (). Value ("a")

<name>a</name> form, with Readelementtext

When reading, it is through the loop, while (!reader.atend ()) {reader.readnext ();

qfile file ("Test.txt"); File.Open (qiodevice::readonly);

Qxmlstreamreader Reader (&file);
	while (!reader.atend ()) {reader.readnext (); if (Reader.isstartelement ()) {if (reader.name () = = "Round") {Qdebug () << reader.attributes (). Value ("id"). toSt
			Ring ();
		Qdebug () << reader.attributes (). Value ("TableID"). ToString ();
			} else if (reader.name () = = "Player1") {qdebug () << reader.attributes (). Value ("Framescore"). ToString ();
			Qdebug () << reader.attributes (). Value ("Points"). ToString ();
			Qdebug () << reader.attributes (). Value ("Break"). ToString ();
		Qdebug () << reader.readelementtext ();
			} else if (reader.name () = = "Player2") {qdebug () << reader.attributes (). Value ("Framescore"). ToString ();
			Qdebug () << reader.attributes (). Value ("Points"). ToString ();
			Qdebug () << reader.attributes (). Value ("Break"). ToString ();
		Qdebug () << reader.readelementtext (); }}} file.close (); 
The results are as follows

"" "1" "" "" "" " 
Hello World 
" 
"" "" " 







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.