此图为xml文件格式 下图为生成此xml的代码
bool Instance::write_xml(QString path) { QFile file(path); if (!file.open(QFile::ReadOnly | QFile::Text)) { qDebug() << "open for add error..."; return false; } QDomDocument doc; QString errorStr; int errorLine; int errorColumn; if (!doc.setContent(&file, false, &errorStr, &errorLine, &errorColumn)) { qDebug() << "add setcontent error..." << "errorStr&&&&" << errorStr << "errorLine" << errorLine << "errorColume" << errorColumn; //file.close(); } file.close(); QDomElement root = doc.documentElement(); if (root.isNull()) { root = doc.createElement("configinfo"); } QDomElement elementRoot = doc.createElement("url"); QDomElement elementCreateRoute = doc.createElement("CreateRouteURL"); QDomElement elementCreateAssignment = doc.createElement("CreateAssignmentURL"); QDomElement elementDownLoadImage = doc.createElement("DownLoadImageURL"); QDomElement elementGetTowerID = doc.createElement("GetTowerIDURL"); QDomElement elementDeleteTower = doc.createElement("DeleteTowerIDURL"); QDomElement elementGetLineID = doc.createElement("GetLineIDURL"); QDomElement elementGetResult = doc.createElement("GetResultURL"); QDomElement elementRealTime = doc.createElement("RealTimeURL"); QDomElement elementDeleteAssignment = doc.createElement("DeleteAssignmentIDURL"); QDomElement elementA = doc.createElement("a"); QDomElement elementB = doc.createElement("b"); QDomElement elementC = doc.createElement("c"); QDomElement elementD = doc.createElement("d"); QDomElement elementE = doc.createElement("e"); QDomElement elementF = doc.createElement("f"); QDomElement elementG = doc.createElement("g"); QDomElement elementH = doc.createElement("h"); QDomElement elementI = doc.createElement("i"); QDomText text; text = doc.createTextNode("http:xxxxxxxxxxxxxxxxxxxxxxxxxxxx"); elementA.appendChild(text); elementCreateRoute.appendChild(elementA); text = doc.createTextNode("http:xxxxxxxxxxxxxxxxxxxxxxxxx"); elementB.appendChild(text); elementCreateAssignment.appendChild(elementB); text = doc.createTextNode("http:xxxxxxxxxxxxxxxxxxxxxxxx"); elementC.appendChild(text); elementDownLoadImage.appendChild(elementC); text = doc.createTextNode("http:xxxxxxxxxxxxxxxx"); elementD.appendChild(text); elementGetTowerID.appendChild(elementD); text = doc.createTextNode("http:xxxxxxxxxxxxxxxxxxxx"); elementE.appendChild(text); elementDeleteTower.appendChild(elementE); text = doc.createTextNode("http:xxxxxxxxxxxxxxxxxx"); elementF.appendChild(text); elementGetLineID.appendChild(elementF); text = doc.createTextNode("http:xxxxxxxxxxxxxxxxxxxxx"); elementG.appendChild(text); elementGetResult.appendChild(elementG); text = doc.createTextNode("http:xxxxxxxxxxxxxxxxxx"); elementH.appendChild(text); elementRealTime.appendChild(elementH); text = doc.createTextNode("http:xxxxxxxxxxxxxxxxxxx"); elementI.appendChild(text); elementDeleteAssignment.appendChild(elementI); text.clear(); elementRoot.appendChild(elementCreateRoute); elementRoot.appendChild(elementCreateAssignment); elementRoot.appendChild(elementDownLoadImage); elementRoot.appendChild(elementGetTowerID); elementRoot.appendChild(elementDeleteTower); elementRoot.appendChild(elementGetLineID); elementRoot.appendChild(elementGetResult); elementRoot.appendChild(elementRealTime); elementRoot.appendChild(elementDeleteAssignment); root.appendChild(elementRoot); if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { qDebug() << "open for add error!!"; } QTextStream out(&file); doc.save(out, 4); file.close(); return true; }贴上读取此xml代码
QString Instance::read_xml(QString path, QString tempelement) { QDomDocument doc; QFile file(path); if (!file.open(QIODevice::ReadOnly)) { return "error"; } if (!doc.setContent(&file)) { file.close(); return "error"; } file.close(); QDomElement root = doc.documentElement();//读取根节点 QDomNode node = root.firstChild();//读取第一个子节点 node = node.toElement().firstChild(); while (!node.isNull()) { QString tagName = node.toElement().tagName(); if (tagName.compare(tempelement) == 0 && tagName.compare("CreateRouteURL") == 0) //节点标记查找 { return node.toElement().firstChild().toElement().text(); } else if (tagName.compare(tempelement) == 0 && tagName.compare("CreateAssignmentURL") == 0) { return node.toElement().firstChild().toElement().text(); } else if (tagName.compare(tempelement) == 0 && tagName.compare("DownLoadImageURL") == 0) { return node.toElement().firstChild().toElement().text(); } else if (tagName.compare(tempelement) == 0 && tagName.compare("GetTowerIDURL") == 0) { return node.toElement().firstChild().toElement().text(); } else if (tagName.compare(tempelement) == 0 && tagName.compare("DeleteTowerIDURL") == 0) { return node.toElement().firstChild().toElement().text(); } else if (tagName.compare(tempelement) == 0 && tagName.compare("GetLineIDURL") == 0) { return node.toElement().firstChild().toElement().text(); } else if (tagName.compare(tempelement) == 0 && tagName.compare("GetResultURL") == 0) { return node.toElement().firstChild().toElement().text(); } else if (tagName.compare(tempelement) == 0 && tagName.compare("RealTimeURL") == 0) { return node.toElement().firstChild().toElement().text(); } else if (tagName.compare(tempelement) == 0 && tagName.compare("DeleteAssignmentIDURL") == 0) { return node.toElement().firstChild().toElement().text(); } node = node.nextSibling();//读取下一个兄弟节点 } return "error"; }