QT xml讀寫操作

2021-10-08 03:35:52 字數 2161 閱讀 1957

main.cpp中

#include

#include

#include

#include

#include

intmain

(int argc,

char

*ar**)

qxmlstreamwriter stream

(&file)

; stream.

setcodec

(qtextcodec::

codecforname

("utf-8"))

; stream.

setautoformatting

(true);

//開啟自動格式化

stream.

writestartdocument()

;//從文件開始

stream.

writestartelement

("teachers");

stream.

writestartelement

("teacher");

stream.

writeattribute

("id"

,"01");

stream.

writetextelement

("name"

,"老王");

stream.

writetextelement

("book"

,"金瓶梅");

stream.

writestartelement

("teacher");

stream.

writeattribute

("id"

,"02");

stream.

writetextelement

("name"

,"老是");

stream.

writetextelement

("book"

,"金瓶梅");

stream.

writestartelement

("teacher");

stream.

writeattribute

("id"

,"03");

stream.

writetextelement

("name"

,"老六");

stream.

writetextelement

("book"

,"金瓶梅");

stream.

writeendelement()

; stream.

writeenddocument()

; file.

close()

;qdebug()

<<

"存入檔案成功"

; qfile file1

("./testwrite.xml");

if(!file1.

open

(qfile::readonly|qfile::text)

) qxmlstreamreader reader;

reader.

setdevice

(&file1)

;while

(!reader.

atend()

)if(type==qxmlstreamreader::endelement)

qdebug()

<<

"<< reader.

name()

<<

">";if

(type == qxmlstreamreader::characters &&

!reader.

iswhitespace()

)//會字元空白

qdebug()

<< reader.

text()

;// 如果讀取過程**現錯誤,那麼輸出錯誤資訊

if(reader.

haserror()

)}file1.

close()

;return a.

exec()

;}

qt xml解析大檔案

最近專案中遇到了解析xml檔案的問題,於是便想在網上搬磚來解決一下,找了一下,c 的大都是tinyxml庫,qt的也是用document,都是解析小檔案的xml。大檔案的還不好找解析方式。於是就看到了用stream來解析,親自試了一下,超級簡單,方便。下面附上 include include inc...

讀寫檔案操作

匯入領導簡歷資訊 return public string leaderresumeimport throws ioexceptionelse if fr null fr.close if flag else fw.close string sjson outputclient sjson retu...

nand flash 讀寫操作

1 頁讀 如下所示,先輸入讀命令 0x00 然後輸入5個位址,分為2個列位址 頁內位址 和3個行位址 頁和block位址 再輸入讀確認位址0x30,nand硬體會自動從指定的頁的主儲存區讀資料到頁快取 頁暫存器 此時r b 為低電平,當內部讀完成後,r b 恢復為高電平,此時便可以通過nfc的資料暫...