基于c++QT5 文件的基本操作
#include <QCoreApplication>
#include <QtXml>
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;
}