
背景我需要解決一個(gè)bug需要我從xml中讀取數(shù)據(jù)到QDomDocument然后獲取到我想要的目標(biāo)信息然后修改該信息。---------------------------------------------------------------------------------------------------------Qt QDomComment讀寫Xml文件(含示例源碼)_qt qdomdocument 操做xml-CSDN博客QDomDocument類可以方便地讀取和操作XML文件。優(yōu)點(diǎn)易于使用XML文檔缺點(diǎn)是相對(duì)較慢因?yàn)榛贒OM)。---------------------------------------------------------------------------------------------------示例Xml文檔XML 教程 | 菜鳥教程 (runoob.com)XML 文檔第一行以 XML 聲明開始用來(lái)表述文檔的一些信息如?xml version1.0 encodingUTF-8?XML 文檔實(shí)例?xml version1.0 encodingUTF-8? site nameRUNOOB/name urlhttps://www.runoob.com/url logorunoob-logo.png/logo desc編程學(xué)習(xí)網(wǎng)站/desc /sitename、url、logo、desc分別為標(biāo)簽標(biāo)簽內(nèi)包含了要傳遞的信息。它必須成對(duì)出現(xiàn)有開始標(biāo)簽就需要有結(jié)束標(biāo)簽。class student sex男 age18 id01/id name張三/name /student student sex女 age28 id02/id name李四/name /student /class例如classxxx/classstudentyyy/studentXml文件必須包含根元素它是所有其它元素的父元素XML文檔由元素構(gòu)成每個(gè)元素包含開始標(biāo)簽結(jié)束標(biāo)簽和元素內(nèi)容。例如id02/id讀取Xml文件的信息//XML文件路徑 QString path QCoreApplication::applicationDirPath()/xxx.xml; qDebug()path; //打開XML文件 QFile file(path); if(!file.open(QIODevice::ReadOnly)) { qDebug()File open faild!; return-1; } //創(chuàng)建QDomDocument對(duì)象用于解析XML QDomDocument doc; //通過(guò)QFile對(duì)象設(shè)置QDomDocument的內(nèi)容 if(!doc.setContent(file)) { file.close(); return -1; } //關(guān)閉文件 file.close(); //解析XML //獲取XML根結(jié)點(diǎn) QDomElement root doc.documentElement(); //遍歷每個(gè)student結(jié)點(diǎn) //通過(guò)標(biāo)簽名獲取結(jié)點(diǎn)列表 QDomNodeList studentList root.elementsByTagName(student); int listSize studentList.size(); for(int i 0; i listSize; i) { //通過(guò)索引獲取QDomElement對(duì)象 QDomElement student studentList.at(i).toElement(); //獲取sex屬性值 QString sex student.attribute(sex); //獲取age屬性值 QString age student.attribute(age); /* *函數(shù)原型為 *QDomElement firstChildElement(const QString tagName QString()) const; *解析:參數(shù)tagName是可選的表示要查詢的子元素結(jié)點(diǎn)的標(biāo)簽名。如果指定了tagName則返回 *第一個(gè)匹配標(biāo)簽的子元素結(jié)點(diǎn)如果沒有指定tagName則返回第一個(gè)子元素結(jié)點(diǎn)。 **/ QString studentID student.firstChildElement(id).text(); //獲取id元素節(jié)點(diǎn)的文本內(nèi)容 QString studentName student.firstChildElement(name).text(); //獲取name元素節(jié)點(diǎn)的文本內(nèi)容 qDebug()student:; qDebug()sex:sex; qDebug()age:age; qDebug()studentID:studentID; qDebug()studentName:studentName; }寫入Xml文件代碼來(lái)自《Qt Creator快速入門》)QDomDocument doc; QDomProcessingInstruction instruction; instruction doc.createProcessingInstruction(xml,version \1.0\ encoding \UTF-8\); doc.appendChild(instruction); QDomElement root doc.createElement(QString(書庫(kù))); doc.appendChild(root); QDomElement book doc.createElement(QString(圖書)); QDomAttr id doc.createAttribute(QString(編號(hào))); id.setValue(1); book.setAttributeNode(id); QDomText text; QDomElement title doc.createElement(QString(書名)); text doc.createTextNode(QString(Qt)); title.appendChild(text); book.appendChild(title); QDomElement author doc.createElement(QString(作者)); text doc.createTextNode(QString(shiming)); author.appendChild(text); book.appendChild(author); root.appendChild(book); QFile file(my.xml); if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate))return 0; QTextStream out(file); doc.save(out,4); file.close();修改書名為《Qt Creator快速入門》 作者改為霍亞飛//先把信息讀取出來(lái) //XML文件路徑 QFile file(my.xml); if(!file.open(QIODevice::ReadOnly)) { qDebug()File open faild!; return -1; } //創(chuàng)建QDomDocument對(duì)象用于解析XML QDomDocument doc; //通過(guò)QFile對(duì)象設(shè)置QDomDocument的內(nèi)容 if(!doc.setContent(file)) { file.close(); return -1; } //關(guān)閉文件 file.close(); //解析XML //獲取XML根結(jié)點(diǎn) QDomElement root doc.documentElement(); QDomElement book root.firstChildElement(圖書); QDomElement bookName book.firstChildElement(書名); bookName.firstChild().setNodeValue(Qt Creator快速入門); QDomElement author book.firstChildElement(霍亞飛); author.firstChild().setNodeValue(sfsefes); if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate))return 0; QTextStream out(file); doc.save(out,4); file.close();這里我繼續(xù)使用Qt的.ui文件作為示例它是以Xml文件的形式進(jìn)行存儲(chǔ)的?xml version1.0 encodingUTF-8? ui version4.0 classMainWindow/class widget classQMainWindow nameMainWindow property namegeometry rect x0/x y0/y width731/width height460/height /rect /property property namewindowTitle stringGraphics View繪圖/string /property widget classQWidget namecentralWidget widget classQWGraphicsView nameView property namegeometry rect x10/x y10/y width600/width height400/height /rect /property property namerenderHints setQPainter::Antialiasing|QPainter::TextAntialiasing/set /property property namedragMode enumQGraphicsView::RubberBandDrag/enum /property /widget /widget widget classQMenuBar namemenuBar property namegeometry rect x0/x y0/y width731/width height30/height /rect /property /widget widget classQToolBar namemainToolBar property nameiconSize size width16/width height16/height /size /property property nametoolButtonStyle enumQt::ToolButtonTextUnderIcon/enum /property attribute nametoolBarArea enumTopToolBarArea/enum /attribute attribute nametoolBarBreak boolfalse/bool /attribute addaction nameactZoomIn/ addaction nameactZoomOut/ addaction nameactRestore/ addaction nameseparator/ addaction nameactRotateLeft/ addaction nameactRotateRight/ addaction nameactEdit_Front/ addaction nameactEdit_Back/ addaction nameactGroup/ addaction nameactGroupBreak/ addaction nameseparator/ addaction nameactEdit_Delete/ addaction nameseparator/ addaction nameactQuit/ /widget widget classQStatusBar namestatusBar/ widget classQToolBar nametoolBar property namewindowTitle stringtoolBar/string /property property nameallowedAreas setQt::LeftToolBarArea/set /property property nameiconSize size width16/width height16/height /size /property property nametoolButtonStyle enumQt::ToolButtonTextUnderIcon/enum /property attribute nametoolBarArea enumLeftToolBarArea/enum /attribute attribute nametoolBarBreak boolfalse/bool /attribute addaction nameactItem_Rect/ addaction nameactItem_Ellipse/ addaction nameactItem_Circle/ addaction nameactItem_Triangle/ addaction nameactItem_Polygon/ addaction nameactItem_Line/ addaction nameactItem_Text/ /widget action nameactItem_Rect property nameicon iconset resourceres.qrc normaloff:/images/images/RECTANGL.BMP/normaloff:/images/images/RECTANGL.BMP/iconset /property property nametext string矩形/string /property property nametoolTip string添加矩形/string /property /action action nameactItem_Ellipse property nameicon iconset resourceres.qrc normaloff:/images/images/ELLIPSE.BMP/normaloff:/images/images/ELLIPSE.BMP/iconset /property property nametext string橢圓/string /property property nametoolTip string添加橢圓型/string /property /action action nameactItem_Line property nameicon iconset resourceres.qrc normaloff:/images/images/LINE.BMP/normaloff:/images/images/LINE.BMP/iconset /property property nametext string直線/string /property property nametoolTip string添加直線/string /property /action action nameactEdit_Delete property nameicon iconset resourceres.qrc normaloff:/images/images/108.bmp/normaloff:/images/images/108.bmp/iconset /property property nametext string刪除/string /property property nametoolTip string刪除選中的圖元/string /property /action action nameactQuit property nameicon iconset resourceres.qrc normaloff:/images/images/132.bmp/normaloff:/images/images/132.bmp/iconset /property property nametext string退出/string /property property nametoolTip string退出本系統(tǒng)/string /property /action action nameactItem_Text property nameicon iconset resourceres.qrc normaloff:/images/images/800.bmp/normaloff:/images/images/800.bmp/iconset /property property nametext string文字/string /property property nametoolTip string添加文字/string /property /action action nameactEdit_Front property nameicon iconset resourceres.qrc normaloff:/images/images/528.bmp/normaloff:/images/images/528.bmp/iconset /property property nametext string前置/string /property property nametoolTip string居于最前面/string /property /action action nameactEdit_Back property nameicon iconset resourceres.qrc normaloff:/images/images/526.bmp/normaloff:/images/images/526.bmp/iconset /property property nametext string后置/string /property property nametoolTip string居于最后面/string /property /action action nameactItem_Polygon property nameicon iconset resourceres.qrc normaloff:/images/images/FREEFORM.BMP/normaloff:/images/images/FREEFORM.BMP/iconset /property property nametext string梯形/string /property property nametoolTip string添加梯形/string /property /action action nameactZoomIn property nameicon iconset resourceres.qrc normaloff:/images/images/zoomin.png/normaloff:/images/images/zoomin.png/iconset /property property nametext string放大/string /property property nametoolTip string放大/string /property /action action nameactZoomOut property nameicon iconset resourceres.qrc normaloff:/images/images/zoomout.png/normaloff:/images/images/zoomout.png/iconset /property property nametext string縮小/string /property property nametoolTip string縮小/string /property /action action nameactRotateLeft property nameicon iconset resourceres.qrc normaloff:/images/images/rotateleft.png/normaloff:/images/images/rotateleft.png/iconset /property property nametext string左旋轉(zhuǎn)/string /property property nametoolTip string左旋轉(zhuǎn)/string /property /action action nameactRotateRight property nameicon iconset resourceres.qrc normaloff:/images/images/rotateright.png/normaloff:/images/images/rotateright.png/iconset /property property nametext string右旋轉(zhuǎn)/string /property property nametoolTip string右旋轉(zhuǎn)/string /property /action action nameactRestore property nameicon iconset resourceres.qrc normaloff:/images/images/420.bmp/normaloff:/images/images/420.bmp/iconset /property property nametext string恢復(fù)/string /property property nametoolTip string恢復(fù)大小/string /property /action action nameactGroup property nameicon iconset resourceres.qrc normaloff:/images/images/UNGROUP.BMP/normaloff:/images/images/UNGROUP.BMP/iconset /property property nametext string組合/string /property property nametoolTip string組合/string /property /action action nameactGroupBreak property nameicon iconset resourceres.qrc normaloff:/images/images/128.bmp/normaloff:/images/images/128.bmp/iconset /property property nametext string打散/string /property property nametoolTip string取消組合/string /property /action action nameactItem_Circle property nameicon iconset resourceres.qrc normaloff:/images/images/08.JPG/normaloff:/images/images/08.JPG/iconset /property property nametext string圓形/string /property property nametoolTip string圓形/string /property /action action nameactItem_Triangle property nameicon iconset resourceres.qrc normaloff:/images/images/Icon1242.ico/normaloff:/images/images/Icon1242.ico/iconset /property property nametext string三角形/string /property property nametoolTip string三角形/string /property /action /widget layoutdefault spacing6 margin11/ customwidgets customwidget classQWGraphicsView/class extendsQGraphicsView/extends header locationglobalqwgraphicsview.h/header /customwidget /customwidgets resources include locationres.qrc/ /resources connections connection senderactQuit/sender signaltriggered()/signal receiverMainWindow/receiver slotclose()/slot hints hint typesourcelabel x-1/x y-1/y /hint hint typedestinationlabel x302/x y153/y /hint /hints /connection /connections /ui還是有難度的。最外層是標(biāo)簽ui內(nèi)部是classwidgetlayoutdefaultcustomwidgetsresourcesconnections------------------------------------------------widget內(nèi)包含propertywidgetaction--------------------------------------------------------我讀取一下property namegeometry rect x0/x y0/y width731/width height460/height /rect /property property namewindowTitle stringGraphics View繪圖/string /property這一段信息吧讀取.ui文件的代碼//解析XML QDomNode firstNode doc.firstChild(); qDebug()版本和編碼信息firstNode.nodeName()firstNode.nodeValue(); // xml version1.0 encodingUTF-8 qDebug()是否是Xml說(shuō)明firstNode.isProcessingInstruction(); QDomElement ui doc.documentElement(); qDebug()root tag:ui.tagName(); qDebug()root nodeType:ui.nodeType(); //QDomNode::ElementNode 1 qDebug()root hasChildNodes:ui.hasChildNodes(); //true qDebug()root childNodes count:ui.childNodes().count(); //6 qDebug()root hasAttributes:ui.hasAttributes(); //true qDebug()root hasAttribute version:ui.hasAttribute(version); //true QDomElement xWidget ui.firstChildElement(widget); qDebug()widget hasAttribute name:xWidget.hasAttribute(name); //true qDebug()widget hasAttribute class:xWidget.hasAttribute(class); //true QDomElement xProperty xWidget.firstChildElement(property); while(!xProperty.isNull()) { qDebug()xProperty.attribute(name); xProperty xProperty.nextSiblingElement(property); }