Qt之XML文件操作

XML文件格式

<?xml version="1.0" encoding="UTF-8"?>
<XML>
    <a>
        <b>1</b>
        <c>1</c>
        <d>13</d>
        <e>牛逼</e>
    </a>
    <a>
        <b>1</b>
        <c>1</c>
        <d>14</d>
        <e>这牛逼</e>
    </a>
</XML>

读XML文件

bool EXPORT_XML_WIDGET::readXML()
  {
      DATA temp;//结构体DATA
      QString dirPath = QFileDialog::getOpenFileName(this,"open","H:/","XML files(*.xml)");
      QDomDocument doc;
      QFile file(dirPath);
      if (!file.open(QIODevice::ReadOnly))
      {
          QMessageBox::information(this,tr("提示"),tr("打开XML文件失败!"),QMessageBox::Yes);
          return false;
      }

      if (!doc.setContent(&file))
      {
          file.close();
          return false;
      }
      file.close();
      QDomElement child = doc.documentElement();//读取根节点
      if(child.toElement().tagName() == "XML")
      {
          QDomNode child_1 = child.toElement().firstChild();
          while(!child_1.isNull())
          {
              if(child_1.toElement().tagName() == "a")
              {
                  QDomNode child_2 = child_1.toElement().firstChild();
                  while(!child_2.isNull())
                  {
                      if(child_2.toElement().tagName() == "b")
                      {
                          if(child_2.toElement().text().toInt() == 1)
                          {
                              temp.type = "直流";
                          }
                          else
                          {
                              temp.type = "交流";
                          }

                      }
                      else if(child_2.toElement().tagName() == "c")
                      {
                          temp.seriNo = child_2.toElement().text().toInt()+1;
                      }
                      else if(child_2.toElement().tagName() == "d")
                      {
                          temp.branchNo = child_2.toElement().text().toInt()+1;
                      }
                      else if(child_2.toElement().tagName() == "e")
                      {
                          temp.name = child_2.toElement().text();
                          adddata_Arr.append(temp);  //adddate_Arr为QVector
                      }
                      child_2 = child_2.nextSibling();
                  }
              }
              child_1 = child_1.nextSibling();
          }

      }
      return true;
  }

写XML文件

void EXPORT_XML_WIDGET::writeXML()
 {
       QVector<DATA>::iterator iter;
     //打开或创建文件
       QString dir = QFileDialog::getExistingDirectory(this, tr("选择存放路径"),
                                                         "H:/",
                                                         QFileDialog::ShowDirsOnly
                                                         | QFileDialog::DontResolveSymlinks);
       QString dirPath;
       dirPath=tr("%1/write.xml").arg(dir);
       QFile file(dirPath);
       if(!file.open(QIODevice::ReadWrite|QIODevice::Text|QIODevice::Truncate))
       {
           qDebug() << "File open error";
           return;
       } else
       {
           qDebug() <<"File open!";
       }
       QDomDocument doc;
       //写入xml头部
       QDomProcessingInstruction instruction; //添加处理命令
       instruction=doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"UTF-8\"");
       doc.appendChild(instruction);
      //添加根节点
       QDomElement root=doc.createElement("XML");
       doc.appendChild(root);
      //添加第一个子节点及其子元素

     for (iter=adddata_Arr.begin();iter!=adddata_Arr.end();iter++)
     {

            QDomElement change=doc.createElement("a");

            QDomElement b=doc.createElement("b"); //创建子元素
            QDomText text;
            if(iter->type == "直流")
            {

                text=doc.createTextNode(QString::number(1));
                b.appendChild(text);
                a.appendChild(type);
            }
            else
            {
                text=doc.createTextNode(QString::number(0));
                b.appendChild(text);
                a.appendChild(b);

            }


            QDomElement c=doc.createElement("c"); //创建子元素
            text=doc.createTextNode(QString::number(iter->seriNo - 1));
            c.appendChild(text);
            a.appendChild(c);

            QDomElement d = doc.createElement("d"); //创建子元素
            text=doc.createTextNode(QString::number(iter->branchNo - 1));
            d.appendChild(text);
            a.appendChild(d);

            QDomElement e = doc.createElement("e"); //创建子元素
            text=doc.createTextNode(iter->name);
            e.appendChild(text);
            a.appendChild(e);


            root.appendChild(a);
     }
          //输出到文件
            QTextStream out_stream(&file);
            out_stream.setCodec("UTF-8");
            doc.save(out_stream,4); //缩进4格
            file.close();


 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值