QT自带例子:
C:\Qt\Qt5.5.1\Examples\Qt-5.5\widgets\itemviews\simpledommodel\simpledommodel.pro
C:\Qt\Qt5.5.1\Examples\Qt-5.5\xml\dombookmarks\dombookmarks.pro
readXmlFile(m_initFolderPath+"/"+m_xmlFileName);
void MainWindow::readXmlFile(QString fileName) { QFile file(fileName); if(!file.open(QFile::ReadOnly | QFile::Text)) { qDebug()<<"open file"<<fileName<<"Failed,error:"<<file.errorString(); return; } QDomDocument document; QString strError; int errLin = 0,errCol = 0; if( !document.setContent(&file,false,&strError,&errLin,&errCol) ) { qDebug()<<"parse file Failed at line"<<errLin<<",column"<<errCol<<","<<strError; return; } if( document.isNull() ) { qDebug()<<"document is null !"; return; } QDomElement root = document.documentElement(); qDebug()<<root.tagName(); QDomElement catalogs = root.firstChildElement(); if( catalogs.isNull() ) { return; } else { //qDebug()<<catalogs.tagName(); } while(!catalogs.isNull()) { foreachElement(catalogs); catalogs = catalogs.nextSiblingElement(); } file.close(); } void MainWindow::foreachElement(QDomElement catalogs) { if(catalogs.tagName()=="") return; //qDebug()<<catalogs.tagName(); if(catalogs.hasAttributes()) { QDomNamedNodeMap map = catalogs.attributes(); if(catalogs.tagName()==NodeSystemButton) { createSystemButton(map); } else if(catalogs.tagName()==NodeSAction) { createSAction(map); } else if(catalogs.tagName()==NodeRibbonPageSystemPopup) { createRibbonPageSystemPopup(map); } else if(catalogs.tagName()==NodeSRAction) { createSRAction(map); } else if(catalogs.tagName()==NodeRibbonQuickAccessBar) { createQuickAccessBar(map); } else if(catalogs.tagName()==NodeExitsAction) { bindingExitsAction(map); } else if(catalogs.tagName()==NodeQuickAction) { createQuickAction(map); } else if(catalogs.tagName()==NodeRibbonPage) { createRibbonPage(map); } else if(catalogs.tagName()==NodeRibbonGroup) { createRibbonGroup(map); } else if(catalogs.tagName()==NodeOptionButtonAction) { } else if(catalogs.tagName()==NodeQMenu) { createPMenu(map); } else if(catalogs.tagName()==NodeQAction) { createPMQAction(map); } else if(catalogs.tagName()==NodePageQAction) { createPageQAction(map); } else if(catalogs.tagName()==NodeRibbonStatusBar) { createRibbonStatusBar(); } else if(catalogs.tagName()==NodeStatusAction) { createStatusAction(map); } } if(catalogs.hasChildNodes()) { for(int i=0;i<catalogs.childNodes().length();++i) { foreachElement(catalogs.childNodes().at(i).toElement()); } } } QString MainWindow::getNodeValue(QDomNamedNodeMap map,QString key) { QString tempValue=map.namedItem(key).nodeValue(); if(key=="Icon") tempValue=m_initFolderPath+"/"+m_imageFolder+"/"+tempValue; return tempValue; }