Qt QDomDocument讀寫XML文件


注意事項

1、在*.pro文件中添加 QT += xml;
2、xml文件中只能存在一個根節點,如果存在根節點的兄弟節點,則只能讀取到第一個根節點的內容。

 1 #include <QDomDocument>
 2 #include <QFile>
 3 #include <QTextStream>
 4 #include <QDebug>
 5 
 6 int main(int argc, char *argv[])  7 {  8     //1.創建XML文件
 9     QString strFile = QString("../test.xml");  10     if(QFile::exists(strFile)) //如果文件已存在,進行刪除
 11  {  12  QFile::remove(strFile);  13  }  14 
 15  QFile file(strFile);  16     if(!file.open(QIODevice::ReadWrite))  17  {  18         return -1; //新建文件打開失敗
 19  }  20 
 21     QTextStream stream(&file);  22     stream.setCodec("UTF-8"); //使用utf-8格式  23 
 24     //xml文件中只能有一個根節點,如果存在多個根節點則讀取時只能讀到第一個節點的內容
 25  QDomDocument doc;  26     QDomProcessingInstruction xmlInstruction = doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"");  27     QDomComment comment = doc.createComment(QString::fromUtf8("這是備注"));  28     QDomProcessingInstruction styleInstruction = doc.createProcessingInstruction("說明", QString::fromUtf8("需要說明的內容"));  29     doc.appendChild(xmlInstruction);  // 開始文檔(XML 聲明)
 30     doc.appendChild(comment);  // 注釋
 31  doc.appendChild(styleInstruction);  32 
 33     //添加根節點
 34     QDomElement root = doc.createElement("root");  35     root.setAttribute("id", "1234");  36  doc.appendChild(root);  37 
 38     QDomElement name = doc.createElement("Name");  39     name.setAttribute("name", QString::fromUtf8("小明")); //向Name節點中添加屬性值
 40     root.appendChild(name); //將Name節點添加到root節點上
 41 
 42     QDomElement gender = doc.createElement(QString::fromUtf8("性別"));  43     gender.setAttribute("gender", QString::fromUtf8(""));  44  root.appendChild(gender);  45 
 46     QDomElement age = doc.createElement("Age");  47     age.setAttribute(QString::fromUtf8("年齡"), "23");  48  root.appendChild(age);  49 
 50     QDomElement math = doc.createElement("math");  51     QDomText text = doc.createTextNode(QString::fromUtf8("一百"));  52     math.appendChild(text); //向math節點添加節點內容
 53  root.appendChild(math);  54 
 55     QDomElement chinese = doc.createElement(QString::fromUtf8("語文"));  56     text = doc.createTextNode("100");  57  chinese.appendChild(text);  58  root.appendChild(chinese);  59 
 60     //刪除節點  61 // root.removeChild(age); //從root節點中刪除子節點Age  62 
 63     //保存到xml文件
 64     doc.save(stream, 4, QDomNode::EncodingFromTextStream);  65  file.close();  66 
 67     //2.讀取XML文件
 68  file.open(QIODevice::ReadOnly);  69  QDomDocument dom;  70     dom.setContent(&file);  71  file.close();  72 
 73     QDomNodeList list = dom.childNodes();  74     int iCount = list.count();  75     for(int index = 0; index < iCount; index++)  76  {  77  QString strTemp;  78         QDomNode node = list.at(index);  79         if(node.nodeType() == QDomNode::ProcessingInstructionNode)  80  {  81             strTemp = node.toProcessingInstruction().data();  82  }  83         else if(QDomNode::CommentNode == node.nodeType())  84  {  85             strTemp = node.toComment().data();  86  }  87         else
 88  {  89             strTemp = node.toElement().tagName();  90  }  91         qDebug() << strTemp;  92  }  93 
 94     QDomElement element = dom.firstChildElement(); //獲取根節點
 95     QDomElement ageElement = element.firstChildElement("Age"); //獲取名稱為“Age”的第一個子節點
 96     qDebug() << QString::fromUtf8("年齡:") << ageElement.attribute(QString::fromUtf8("年齡"));  97 
 98     QDomElement genderElement = element.firstChildElement(QString::fromUtf8("性別"));  99     qDebug() << QString::fromUtf8("性別:") << genderElement.attribute("gender"); 100 
101     QDomElement nameElement = element.firstChildElement("Name"); 102     qDebug() << QString::fromUtf8("姓名:") << nameElement.attribute("name"); 103 
104     QDomElement Math = element.firstChildElement("math"); 105     qDebug() << QString::fromUtf8("數學:") << Math.text(); 106 
107     QDomElement Chinese = Math.nextSiblingElement(); //獲取Math的下一個兄弟節點
108     qDebug() << QString::fromUtf8("語文:") << Chinese.text(); 109 
110     return 0; 111 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM