Qt下でのXML形式ファイルの読み書き(QdomDocumentクラスを使用)

5186 ワード

簡単に述べる
XMLはタグ言語であり、記憶および伝送情報を構造化するために設計され、一般的なドキュメントデータ構造である.個人的には、Qtの下でXML形式のファイルを読み書きするには、純粋なC++のrapidxmlを使用してソースライブラリを開く3つの方法があります.利点は速度が速く、strlen()に匹敵する速度である.欠点は中国語の処理が面倒で、符号化はANSIフォーマットしかありません.二つ目はQXmlStreamReaderクラスを使用し、QXmlQueryクラスを適切に結合することである.利点は柔軟性が強く、メモリを節約することです.欠点は、理解が比較的難しく、データベース言語の知識が必要であることです.3つ目はQdomDocumentクラスを使用することです.Qtの下でxmlファイルを読み書きする伝統的で普遍的な方法で、欠点は読み書きするファイルが大きいと、大量のメモリ空間を消費することです.QdomDocumentクラスはQFileとして理解でき、このクラスのオブジェクトはファイル全体を表し、通常xmlファイルのプライマリノードでもある.さらに使用するクラスとしては、QdomNode(ノードベースクラス)、QdomElement(ノードクラス)、QdomText(コンテンツ)、QdomAttr(プロパティ)、QdomProcessingInstruction(文書ヘッダ)がある.
コードの道
ユーザー名ファイル「user.xml」を読み書きします.ファイルの内容は次のとおりです.


  
      1
      12
      123
      1
  
  
      2
      22
      234
      1
  


上記のxmlファイルを1つの構造体に読み込む
//       
struct UserStruct
{
  QString username; 
  QString password;
  QString decribe;
  int flag;    //    
  void clear()
  {
    username = "";
    password = "";
    describe = "";
    flag = 1;
  }
};

xml形式の関数コードを読むには、次のようにします.
void readUserxml()
{
  UserStruct tmpInfo;
  QFile file(filename);
  if (!file.open(QIODevice::ReadOnly|QIODevice::Text))
  {
  	return;
  }
  QDomDocument doc;  //xml    
  if (!doc.setContent(&file))
  {
  	file.close();
  	return;
  }
  file.close();
  QDomElement root = doc.documentElement(); //   ALL
    if (!root.isNull())
    {
        QDomNode node_a = root.firstChild(); //  DATA
        while (!node_a.isNull())
        {
            QDomElement node_all = node_a.toElement(); //QDomnode  QDomElement
            if (!node_all.isNull())
            {
                tmpInfo.clear(); //  
                QDomElement n_name = node_all.firstChildElement("Name"); //Name  

                if (!n_name.isNull())
                {
                    tmpInfo.username = n_name.text();
                }
                QDomElement n_passwd = node_all.firstChildElement("Password"); //Password  
                if (!n_passwd.isNull())
                {
                    tmpInfo.password = n_passwd.text();
                }
                QDomElement n_describe = node_all.firstChildElement("Describe"); //Describe  
                if (!n_describe.isNull())
                {
                    tmpInfo.describe = n_describe.text();
                }
                QDomElement n_flag = node_all.firstChildElement("Flag"); //Flag  
                if (!n_flag.isNull())
                {
                    tmpInfo.flag  = n_flag.text().toInt();
                }

             //   //  vector
             //   UserInfo.push_back(tmpInfo);
            }
            node_a = node_a.nextSibling();       //     DATA  
        }
    }
}

XML形式のファイルを書く
void writeUserxml()
{
    //  xml
    QFile file(Filename);
    if (!file.open(QIODevice::WriteOnly|QIODevice::Text|QIODevice::Truncate))
    {
        return;
    }
    QDomDocument doc;
    QDomProcessingInstruction instruction;
    instruction = doc.createProcessingInstruction("xml", "version = \"1.0\" encoding = \"UTF-8\"");
    doc.appendChild(instruction);         //     

    QDomElement root = doc.createElement("ALL");
    doc.appendChild(root);

//    UserStruct tmp;
//    tmp.username = "11";
//    tmp.password = "1234";
//    tmp.describe = "321";
//    tmp.flag = 1;
//    UserInfo.push_back(tmp);

    int sum = UserInfo.size();

//    struct UserStruct
//    {
//        QString username;
//        QString password;
//        QString describe;
//        int flag;
//    };

    for (int i = 0; i < sum; ++i)
    {
        QDomElement node_data = doc.createElement("DATA");
        QDomElement node_name = doc.createElement("Name");
        QDomElement node_passwd = doc.createElement("Password");
        QDomElement node_describe = doc.createElement("Describe");
        QDomElement node_flag = doc.createElement("Flag");

        node_name.appendChild(doc.createTextNode(UserInfo[i].username));
        node_passwd.appendChild(doc.createTextNode(UserInfo[i].password));
        node_describe.appendChild(doc.createTextNode(UserInfo[i].describe));
        node_flag.appendChild(doc.createTextNode(QString::number(UserInfo[i].flag)));

        node_data.appendChild(node_name);
        node_data.appendChild(node_passwd);
        node_data.appendChild(node_describe);
        node_data.appendChild(node_flag);

        root.appendChild(node_data);
    }
    QTextStream out(&file);
    doc.save(out, 4);     //     
    file.close();
}

例ではノード属性には触れていません.次に、ノード属性の書き込みを補足します.
QDomAttr atr = doc.createAttribute("id");
atr.setValue(str_id);
node_date.setAttributeNode(atr);

まとめ
QtではQdomcumentクラスを用いてxmlコードを書く量は比較的少なくないが,構想ははっきりしている.