qt提供了三种方式解析xml,不过如果想实现对xml文件进行增、删、改等操作,还是DOM方式最方便。
项目配置
pro文件里面添加QT+=xml
include <QtXml>,也可以include <QDomDocument>
pro文件:
- QT += core xml
-
- QT -= gui
-
- TARGET = xmltest
- CONFIG += console
- CONFIG -= app_bundle
-
- TEMPLATE = app
-
-
- SOURCES += main.cpp
代码
main.cpp
- #include <QCoreApplication>
- #include <QtXml> //也可以include <QDomDocument>
-
- void WriteXml()
- {
-
- 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("2013/6/13");
- 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);
-
-
- book=doc.createElement("book");
- book.setAttribute("id",2);
- time=doc.createAttribute("time");
- time.setValue("2007/5/25");
- 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();
-
- }
-
- void ReadXml()
- {
-
- 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();
- }
-
- }
-
- void AddXml()
- {
-
- 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","1813/1/27");
- 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);
- root.appendChild(book);
-
- if(!file.open(QFile::WriteOnly|QFile::Truncate))
- return;
-
- QTextStream out_stream(&file);
- doc.save(out_stream,4);
- file.close();
- }
-
- void RemoveXml()
- {
-
- 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")=="2007/5/25")
- root.removeChild(list.at(i));
- }
-
- if(!file.open(QFile::WriteOnly|QFile::Truncate))
- return;
-
- QTextStream out_stream(&file);
- doc.save(out_stream,4);
- file.close();
- }
-
- void UpdateXml()
- {
-
- 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("Emma");
- 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();
- }
-
- int main(int argc, char *argv[])
- {
-
- qDebug()<<"write xml to file...";
- WriteXml();
- qDebug()<<"read xml to display...";
- ReadXml();
- qDebug()<<"add contents to xml...";
- AddXml();
- qDebug()<<"remove contents from xml...";
- RemoveXml();
- qDebug()<<"update contents to xml...";
- UpdateXml();
- return 0;
-
- }
-
写xml
- <?xml version="1.0" encoding="UTF-8"?>
- <library>
- <book id="1" time="2013/6/13">
- <title>C++ primer</title>
- <author>Stanley Lippman</author>
- </book>
- <book id="2" time="2007/5/25">
- <title>Thinking in Java</title>
- <author>Bruce Eckel</author>
- </book>
- </library>
增加内容xml
- <?xml version=‘1.0‘ encoding=‘UTF-8‘?>
- <library>
- <book time="2013/6/13" id="1">
- <title>C++ primer</title>
- <author>Stanley Lippman</author>
- </book>
- <book time="2007/5/25" id="2">
- <title>Thinking in Java</title>
- <author>Bruce Eckel</author>
- </book>
- <book time="1813/1/27" id="3">
- <title>Pride and Prejudice</title>
- <author>Jane Austen</author>
- </book>
- </library>
删除内容xml
- <?xml version=‘1.0‘ encoding=‘UTF-8‘?>
- <library>
- <book time="2013/6/13" id="1">
- <title>C++ primer</title>
- <author>Stanley Lippman</author>
- </book>
- <book time="1813/1/27" id="3">
- <title>Pride and Prejudice</title>
- <author>Jane Austen</author>
- </book>
- </library>
更新xml
- <?xml version=‘1.0‘ encoding=‘UTF-8‘?>
- <library>
- <book id="1" time="2013/6/13">
- <title>C++ primer</title>
- <author>Stanley Lippman</author>
- </book>
- <book id="3" time="1813/1/27">
- <title>Emma</title>
- <author>Jane Austen</author>
- </book>
- </library>