最近,学习了Qt C++对XML的一系列操作。今天,把相关代码和注释贴在这里, 以备后续使用。
什么是 XML
可扩展标记语言(英语:Extensible Markup Language,简称:XML)是一种标记语言。标记指计算机所能理解的信息符号,通过此种标记,计算机之间可以处理包含各种信息的文章等。
——from Wikipedia
Qt 如何操作 XML
.pro 文件
增加 QT += xml
模块。
.cpp 文件
头部 #include <QtXml>
写入 XML
源代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| void Widget::write_xml() { QFile file("test.xml"); if(!file.open(QFile::WriteOnly | QFile::Truncate)) return; QDomDocument doc;
QDomProcessingInstruction instruction; instruction = doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\""); doc.appendChild(instruction);
QDomElement root = doc.createElement("library"); doc.appendChild(root);
QDomElement book = doc.createElement("book"); book.setAttribute("id", 1); QDomAttr time = doc.createAttribute("time"); time.setValue("2020/08/11"); book.setAttributeNode(time);
QDomElement title = doc.createElement("title"); QDomText text; text = doc.createTextNode("C++ primer"); book.appendChild(title); title.appendChild(text);
QDomElement author = doc.createElement("author"); text = doc.createTextNode("Stanley Lippman"); author.appendChild(text); book.appendChild(author); root.appendChild(book);
QDomElement price = doc.createElement("price"); text = doc.createTextNode("$29.9"); price.appendChild(text); book.appendChild(price); root.appendChild(book);
book = doc.createElement("book"); book.setAttribute("id", 2); time = doc.createAttribute("time"); time.setValue("2020/08/10"); book.setAttributeNode(time); title = doc.createElement("title"); text = doc.createTextNode("Thinking in Java"); book.appendChild(title); title.appendChild(text); author = doc.createElement("author"); text = doc.createTextNode("Bruce Eckel"); author.appendChild(text); book.appendChild(author); root.appendChild(book);
QTextStream out_stream(&file); doc.save(out_stream, 4); file.close(); }
|
调试结果 test.xml
1 2 3 4 5 6 7 8 9 10 11 12
| <?xml version="1.0" encoding="UTF-8"?> <library> <book time="2020/08/11" id="1"> <title>C++ primer</title> <author>Stanley Lippman</author> <price>$29.9</price> </book> <book time="2020/08/10" id="2"> <title>Thinking in Java</title> <author>Bruce Eckel</author> </book> </library>
|
读取 XML
源代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| void Widget::read_xml() { QFile file("test.xml"); if(!file.open(QFile::ReadOnly)) { return; }
QDomDocument doc; if(!doc.setContent(&file)) { file.close(); return; } file.close();
QDomElement root = doc.documentElement(); qDebug() << root.nodeName(); QDomNode node = root.firstChild(); while(!node.isNull()) { if(node.isElement()) { QDomElement e = node.toElement(); qDebug() << e.tagName() << " " << e.attribute("id") << " " << e.attribute("time"); QDomNodeList list = e.childNodes(); for (int i = 0; i < list.count(); i++) { QDomNode n = list.at(i); if(node.isElement()) { qDebug() << n.nodeName() << ":" << n.toElement().text(); } } } node = node.nextSibling(); } }
|
增加 XML 内容
源代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| void Widget::add_xml() { QFile file("test.xml"); if(!file.open(QFile::ReadOnly)) { return; }
QDomDocument doc; if(!doc.setContent(&file)) { file.close(); return; } file.close();
QDomElement root = doc.documentElement(); QDomElement book = doc.createElement("book"); book.setAttribute("id", 3); book.setAttribute("time", "1999/09/09"); QDomElement title = doc.createElement("title"); QDomText text; text = doc.createTextNode("Pride and Prejudice"); title.appendChild(text); book.appendChild(title); QDomElement author = doc.createElement("author"); text = doc.createTextNode("Jane Austen"); author.appendChild(text); book.appendChild(author); QDomElement price = doc.createElement("price"); text = doc.createTextNode("$69.9"); price.appendChild(text); book.appendChild(price); root.appendChild(book);
if(!file.open(QFile::WriteOnly | QFile::Truncate)) { return; }
QTextStream out_stream(&file); doc.save(out_stream, 4); file.close(); }
|
删除 XML 内容
源代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| void Widget::remove_xml() { QFile file("test.xml"); if(!file.open(QFile::ReadOnly)) { return; }
QDomDocument doc; if(!doc.setContent(&file)) { file.close(); return; } file.close();
QDomElement root = doc.documentElement(); QDomNodeList list = doc.elementsByTagName("book"); for(int i = 0; i < list.count(); i++) { QDomElement e = list.at(i).toElement(); if(e.attribute("time") == "2020/08/09") { root.removeChild(list.at(i)); break; } }
if(!file.open(QFile::WriteOnly | QFile::Truncate)) { return; }
QTextStream out_stream(&file); doc.save(out_stream, 4); file.close(); }
|
更新 XML 内容
源代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| void Widget::update_xml() { QFile file("test.xml"); if(!file.open(QFile::ReadOnly)) { return; }
QDomDocument doc; if(!doc.setContent(&file)) { file.close(); return; } file.close();
QDomElement root = doc.documentElement(); QDomNodeList list = root.elementsByTagName("book"); QDomNode node = list.at(list.size() - 1).firstChild(); QDomNode oldnode = node.firstChild(); node.firstChild().setNodeValue("Update_success"); QDomNode newnode = node.firstChild(); node.replaceChild(newnode, oldnode);
if(!file.open(QFile::WriteOnly | QFile::Truncate)) { return; } QTextStream out_stream(&file); doc.save(out_stream, 4); file.close(); }
|
今天的分享就到这里,后续我会继续更新我的Qt学习记录~