标签:hello only debug readonly failure 获取 格式 lse err
1、解析
QFile file(ui->comboBox->currentText()); //打开文件 bool isOK = file.open(QIODevice::ReadOnly | QIODevice::Text); if(isOK){ QXmlStreamReader reader(&file); while(!reader.atEnd()) { //判断是否是节点的开始 if(reader.isStartElement()) { QXmlStreamAttributes attributes = reader.attributes(); // headList代表要解析的值的对象 for(int i = 0; i < headList.size(); i++){ QString str = valueList.at(i); QStringList strList = str.split(","); if(reader.name() == headList.at(i)){ for(int j = 0; j < strList.size(); j++){ ui->textEdit_2->append(strList.at(j)+": " + attributes.value(strList.at(j)).toString()); } } } } reader.readNext(); } } else { qDebug()<<"Open file hello.xml failure"; } file.close();
2、XML的生成
//这里是以树的结构来生成XML的,最多3层 QDomElement element, root, cElement,sElement; QDomProcessingInstruction instruction;//写入xml的头部,添加处理命令 instruction = doc.createProcessingInstruction( "xml", "version = \‘1.0\‘ encoding=\‘UTF-8\‘" );//设置xml的版本号和字节的格式 doc.appendChild(instruction);//添加介绍 root = doc.createElement("XML");//设置根节点为COMMAND doc.appendChild(root); for(int i = 0; i < ui->treeWidget->topLevelItemCount(); i++){ // 获取父节点个数 element = doc.createElement( "OBJECT" );//添加根节点下面的元素OBJECT for(int count = 0; count < ui->treeWidget->columnCount(); count++){ // 获取所在列的数据 element.setAttribute("name", ui->treeWidget->topLevelItem(i)->text(count)); root.appendChild(element); } for(int j=0; j < ui->treeWidget->topLevelItem(i)->childCount(); j++){ cElement = doc.createElement("paramer"); for(int count = 0; count < ui->treeWidget->columnCount(); count++){ cElement.setAttribute("name", ui->treeWidget->topLevelItem(i)->child(j)->text(count)); element.appendChild(cElement); } for(int k=0; k < ui->treeWidget->topLevelItem(i)->child(j)->childCount(); j++){ sElement = doc.createElement("packet"); for(int count = 0; count < ui->treeWidget->columnCount(); count++){ sElement.setAttribute("name", ui->treeWidget->topLevelItem(i)->child(j)->child(k)->text(count)); cElement.appendChild(sElement); } } } } QFile file(fileName); if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate)){ qDebug() << file.errorString(); return; } QTextStream out(&file); doc.save(out, 4);
标签:hello only debug readonly failure 获取 格式 lse err
原文地址:https://www.cnblogs.com/caozewen/p/14769316.html