注意事项
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 }