//獲取字符串字段 QString ConfigHelper::GetStringConfigValue(QString str) { if(str == "InitDeviceNo") { return getMacPath(); }else { QString filePath=QString("%1/%2").arg( qApp->applicationDirPath()).arg("config.xml"); QString names=QString::null; ////打開或創建文件 QFile file(filePath); //相對路徑、絕對路徑、資源路徑都行 if(!file.open(QFile::ReadOnly)) { } QDomDocument doc; if(!doc.setContent(&file)) { file.close(); } file.close(); QDomElement root=doc.documentElement(); //返回根節點 QDomNode node=root.firstChild(); //獲得第一個子節點 while(!node.isNull()) //如果節點不空 { if(node.isElement()) //如果節點是元素 { QDomElement e=node.toElement(); //轉換為元素,注意元素和節點是兩個數據結構,其實差不多 if(e.attribute("key")==str) { names=e.attribute("value"); break; } } node=node.nextSibling(); //下一個兄弟節點,nextSiblingElement()是下一個兄弟元素,都差不多 } return names; } }
