背景:
我需要解決一個bug,需要我從xml中讀取數據到QDomDocument,然后獲取到我想要的目標信息,然后修改該信息。?
---------------------------------------------------------------------------------------------------------
Qt QDomComment讀寫Xml文件(含示例源碼)_qt qdomdocument 操做xml-CSDN博客
QDomDocument類可以方便地讀取和操作XML文件。優點:易于使用XML文檔,缺點是相對較慢。
---------------------------------------------------------------------------------------------------
示例Xml文檔?
XML 教程 | 菜鳥教程 (runoob.com)
<class><student sex="男" age="18"><id>01</id><name>張三</name></student><student sex="女" age="28"><id>02</id><name>李四</name></student>
</class>
標簽
標簽內包含了要傳遞的信息
它必須成對出現,有開始標簽就需要有結束標簽。
Xml文件必須包含根元素,它是所有其它元素的父元素
XML文檔由元素構成,每個元素包含開始標簽,結束標簽和元素內容。
例如:<id>02</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;}//創建QDomDocument對象,用于解析XMLQDomDocument doc;//通過QFile對象設置QDomDocument的內容if(!doc.setContent(&file)){file.close();return -1;}//關閉文件file.close();//解析XML//獲取XML根結點QDomElement root = doc.documentElement();//遍歷每個student結點//通過標簽名獲取結點列表QDomNodeList studentList = root.elementsByTagName("student");int listSize = studentList.size();for(int i = 0; i < listSize; ++i){//通過索引獲取QDomElement對象QDomElement student = studentList.at(i).toElement();//獲取sex屬性值QString sex = student.attribute("sex");//獲取age屬性值QString age = student.attribute("age");/**函數原型為*QDomElement firstChildElement(const QString &tagName = QString()) const;*解析:參數tagName是可選的,表示要查詢的子元素結點的標簽名。如果指定了tagName,則返回*第一個匹配標簽的子元素結點;如果沒有指定tagName,則返回第一個子元素結點。**/QString studentID = student.firstChildElement("id").text(); //獲取id元素節點的文本內容QString studentName = student.firstChildElement("name").text(); //獲取name元素節點的文本內容qDebug()<<"student:";qDebug()<<"sex:"<<sex;qDebug()<<"age:"<<age;qDebug()<<"studentID:"<<studentID;qDebug()<<"studentName:"<<studentName;}
寫入Xml文件(代碼來自《Qt Creator快速入門》)
QDomDocument doc;QDomProcessingInstruction instruction;instruction = doc.createProcessingInstruction("xml","version = \"1.0\" encoding = \"UTF-8\"");doc.appendChild(instruction);QDomElement root = doc.createElement(QString("書庫"));doc.appendChild(root);QDomElement book = doc.createElement(QString("圖書"));QDomAttr id = doc.createAttribute(QString("編號"));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快速入門》 ,作者改為霍亞飛
//先把信息讀取出來//XML文件路徑QFile file("my.xml");if(!file.open(QIODevice::ReadOnly)){qDebug()<<"File open faild!";return -1;}//創建QDomDocument對象,用于解析XMLQDomDocument doc;//通過QFile對象設置QDomDocument的內容if(!doc.setContent(&file)){file.close();return -1;}//關閉文件file.close();//解析XML//獲取XML根結點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();
?
這里我繼續使用Qt的.ui文件作為示例(它是以Xml文件的形式進行存儲的)
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0"><class>MainWindow</class><widget class="QMainWindow" name="MainWindow"><property name="geometry"><rect><x>0</x><y>0</y><width>731</width><height>460</height></rect></property><property name="windowTitle"><string>Graphics View繪圖</string></property><widget class="QWidget" name="centralWidget"><widget class="QWGraphicsView" name="View"><property name="geometry"><rect><x>10</x><y>10</y><width>600</width><height>400</height></rect></property><property name="renderHints"><set>QPainter::Antialiasing|QPainter::TextAntialiasing</set></property><property name="dragMode"><enum>QGraphicsView::RubberBandDrag</enum></property></widget></widget><widget class="QMenuBar" name="menuBar"><property name="geometry"><rect><x>0</x><y>0</y><width>731</width><height>30</height></rect></property></widget><widget class="QToolBar" name="mainToolBar"><property name="iconSize"><size><width>16</width><height>16</height></size></property><property name="toolButtonStyle"><enum>Qt::ToolButtonTextUnderIcon</enum></property><attribute name="toolBarArea"><enum>TopToolBarArea</enum></attribute><attribute name="toolBarBreak"><bool>false</bool></attribute><addaction name="actZoomIn"/><addaction name="actZoomOut"/><addaction name="actRestore"/><addaction name="separator"/><addaction name="actRotateLeft"/><addaction name="actRotateRight"/><addaction name="actEdit_Front"/><addaction name="actEdit_Back"/><addaction name="actGroup"/><addaction name="actGroupBreak"/><addaction name="separator"/><addaction name="actEdit_Delete"/><addaction name="separator"/><addaction name="actQuit"/></widget><widget class="QStatusBar" name="statusBar"/><widget class="QToolBar" name="toolBar"><property name="windowTitle"><string>toolBar</string></property><property name="allowedAreas"><set>Qt::LeftToolBarArea</set></property><property name="iconSize"><size><width>16</width><height>16</height></size></property><property name="toolButtonStyle"><enum>Qt::ToolButtonTextUnderIcon</enum></property><attribute name="toolBarArea"><enum>LeftToolBarArea</enum></attribute><attribute name="toolBarBreak"><bool>false</bool></attribute><addaction name="actItem_Rect"/><addaction name="actItem_Ellipse"/><addaction name="actItem_Circle"/><addaction name="actItem_Triangle"/><addaction name="actItem_Polygon"/><addaction name="actItem_Line"/><addaction name="actItem_Text"/></widget><action name="actItem_Rect"><property name="icon"><iconset resource="res.qrc"><normaloff>:/images/images/RECTANGL.BMP</normaloff>:/images/images/RECTANGL.BMP</iconset></property><property name="text"><string>矩形</string></property><property name="toolTip"><string>添加矩形</string></property></action><action name="actItem_Ellipse"><property name="icon"><iconset resource="res.qrc"><normaloff>:/images/images/ELLIPSE.BMP</normaloff>:/images/images/ELLIPSE.BMP</iconset></property><property name="text"><string>橢圓</string></property><property name="toolTip"><string>添加橢圓型</string></property></action><action name="actItem_Line"><property name="icon"><iconset resource="res.qrc"><normaloff>:/images/images/LINE.BMP</normaloff>:/images/images/LINE.BMP</iconset></property><property name="text"><string>直線</string></property><property name="toolTip"><string>添加直線</string></property></action><action name="actEdit_Delete"><property name="icon"><iconset resource="res.qrc"><normaloff>:/images/images/108.bmp</normaloff>:/images/images/108.bmp</iconset></property><property name="text"><string>刪除</string></property><property name="toolTip"><string>刪除選中的圖元</string></property></action><action name="actQuit"><property name="icon"><iconset resource="res.qrc"><normaloff>:/images/images/132.bmp</normaloff>:/images/images/132.bmp</iconset></property><property name="text"><string>退出</string></property><property name="toolTip"><string>退出本系統</string></property></action><action name="actItem_Text"><property name="icon"><iconset resource="res.qrc"><normaloff>:/images/images/800.bmp</normaloff>:/images/images/800.bmp</iconset></property><property name="text"><string>文字</string></property><property name="toolTip"><string>添加文字</string></property></action><action name="actEdit_Front"><property name="icon"><iconset resource="res.qrc"><normaloff>:/images/images/528.bmp</normaloff>:/images/images/528.bmp</iconset></property><property name="text"><string>前置</string></property><property name="toolTip"><string>居于最前面</string></property></action><action name="actEdit_Back"><property name="icon"><iconset resource="res.qrc"><normaloff>:/images/images/526.bmp</normaloff>:/images/images/526.bmp</iconset></property><property name="text"><string>后置</string></property><property name="toolTip"><string>居于最后面</string></property></action><action name="actItem_Polygon"><property name="icon"><iconset resource="res.qrc"><normaloff>:/images/images/FREEFORM.BMP</normaloff>:/images/images/FREEFORM.BMP</iconset></property><property name="text"><string>梯形</string></property><property name="toolTip"><string>添加梯形</string></property></action><action name="actZoomIn"><property name="icon"><iconset resource="res.qrc"><normaloff>:/images/images/zoomin.png</normaloff>:/images/images/zoomin.png</iconset></property><property name="text"><string>放大</string></property><property name="toolTip"><string>放大</string></property></action><action name="actZoomOut"><property name="icon"><iconset resource="res.qrc"><normaloff>:/images/images/zoomout.png</normaloff>:/images/images/zoomout.png</iconset></property><property name="text"><string>縮小</string></property><property name="toolTip"><string>縮小</string></property></action><action name="actRotateLeft"><property name="icon"><iconset resource="res.qrc"><normaloff>:/images/images/rotateleft.png</normaloff>:/images/images/rotateleft.png</iconset></property><property name="text"><string>左旋轉</string></property><property name="toolTip"><string>左旋轉</string></property></action><action name="actRotateRight"><property name="icon"><iconset resource="res.qrc"><normaloff>:/images/images/rotateright.png</normaloff>:/images/images/rotateright.png</iconset></property><property name="text"><string>右旋轉</string></property><property name="toolTip"><string>右旋轉</string></property></action><action name="actRestore"><property name="icon"><iconset resource="res.qrc"><normaloff>:/images/images/420.bmp</normaloff>:/images/images/420.bmp</iconset></property><property name="text"><string>恢復</string></property><property name="toolTip"><string>恢復大小</string></property></action><action name="actGroup"><property name="icon"><iconset resource="res.qrc"><normaloff>:/images/images/UNGROUP.BMP</normaloff>:/images/images/UNGROUP.BMP</iconset></property><property name="text"><string>組合</string></property><property name="toolTip"><string>組合</string></property></action><action name="actGroupBreak"><property name="icon"><iconset resource="res.qrc"><normaloff>:/images/images/128.bmp</normaloff>:/images/images/128.bmp</iconset></property><property name="text"><string>打散</string></property><property name="toolTip"><string>取消組合</string></property></action><action name="actItem_Circle"><property name="icon"><iconset resource="res.qrc"><normaloff>:/images/images/08.JPG</normaloff>:/images/images/08.JPG</iconset></property><property name="text"><string>圓形</string></property><property name="toolTip"><string>圓形</string></property></action><action name="actItem_Triangle"><property name="icon"><iconset resource="res.qrc"><normaloff>:/images/images/Icon1242.ico</normaloff>:/images/images/Icon1242.ico</iconset></property><property name="text"><string>三角形</string></property><property name="toolTip"><string>三角形</string></property></action></widget><layoutdefault spacing="6" margin="11"/><customwidgets><customwidget><class>QWGraphicsView</class><extends>QGraphicsView</extends><header location="global">qwgraphicsview.h</header></customwidget></customwidgets><resources><include location="res.qrc"/></resources><connections><connection><sender>actQuit</sender><signal>triggered()</signal><receiver>MainWindow</receiver><slot>close()</slot><hints><hint type="sourcelabel"><x>-1</x><y>-1</y></hint><hint type="destinationlabel"><x>302</x><y>153</y></hint></hints></connection></connections>
</ui>
還是有難度的。
最外層是標簽ui
內部是
class
widget
layoutdefault
customwidgets
resources
connections
------------------------------------------------
widget內包含
property
widget
action
--------------------------------------------------------
我讀取一下:
<property name="geometry"><rect><x>0</x><y>0</y><width>731</width><height>460</height></rect></property><property name="windowTitle"><string>Graphics View繪圖</string></property>
這一段信息吧
讀取.ui文件的代碼
//解析XMLQDomNode firstNode = doc.firstChild();qDebug()<<"版本和編碼信息:"<<firstNode.nodeName()<<firstNode.nodeValue(); // "xml" "version='1.0' encoding='UTF-8'"qDebug()<<"是否是Xml說明:"<<firstNode.isProcessingInstruction();QDomElement ui = doc.documentElement();qDebug()<<"root tag:"<<ui.tagName();qDebug()<<"root nodeType:"<<ui.nodeType(); //QDomNode::ElementNode 1qDebug()<<"root hasChildNodes:"<<ui.hasChildNodes(); //trueqDebug()<<"root childNodes count:"<<ui.childNodes().count(); //6qDebug()<<"root hasAttributes:"<<ui.hasAttributes(); //trueqDebug()<<"root hasAttribute version:"<<ui.hasAttribute("version"); //trueQDomElement xWidget = ui.firstChildElement("widget");qDebug()<<"widget hasAttribute name:"<<xWidget.hasAttribute("name"); //trueqDebug()<<"widget hasAttribute class:"<<xWidget.hasAttribute("class"); //trueQDomElement xProperty = xWidget.firstChildElement("property");while(!xProperty.isNull()){qDebug()<<xProperty.attribute("name");xProperty = xProperty.nextSiblingElement("property");}