#include "mainwindow.h"
#include <QDomDocument>
#include <QFile>
#include <QTextStream>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
writexml();
//readxml();
// addxml();
//deletexml();
modifyxml();
}
MainWindow::~MainWindow()
{
}
void MainWindow::readxml()
{
QFile file("test.xml");
if(!file.open(QFile::ReadOnly))
{
return;
}
QDomDocument doc;
if(!doc.setContent(&file))//设置test.xml到文档中
{
file.close();
return;
}
QDomElement root = doc.documentElement();//返回根节点
QDomNode first = root.firstChild();//第一个子节点
while(!first.isNull())//如果节点不空
{
if(first.isElement())//如果节点是元素
{
QDomElement e = first.toElement();//转换成子元素
qDebug()<<e.attribute("id");
QDomNodeList list = e.childNodes();
for(int i = 0; i < list.size(); ++i)
{
QDomNode n = list.at(i);
if(n.isElement())
{
QDomElement ee = n.toElement();
qDebug()<<ee.nodeName()<<ee.toElement().text();
}
}
}
first = first.nextSibling();
}
}
void MainWindow::writexml()
{
QDomDocument doc;
QDomProcessingInstruction instruct;
instruct= doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\"");
doc.appendChild(instruct);
QDomElement root = doc.createElement("library");
doc.appendChild(root);
QDomElement book = doc.createElement("book");
book.setAttribute("id",1);
book.setAttribute("time","2013/6/13");
QDomElement title = doc.createElement("title");
QDomText text = doc.createTextNode("C++");
title.appendChild(text);
book.appendChild(title);
root.appendChild(book);
QFile file("test.xml");
if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate |QIODevice::Text))
return ;
QTextStream out(&file);
out.setCodec("UTF-8");
doc.save(out,4,QDomNode::EncodingFromTextStream);
file.close();
}
void MainWindow::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 book2 = doc.createElement("book2");
book2.setAttribute("id",2);
book2.setAttribute("time","2019.2.1");
root.appendChild(book2);//增加
if(!file.open(QFile::WriteOnly|QFile::Truncate))
{
return;
}
//输出
QTextStream stream(&file);
doc.save(stream,4);
file.close();
}
void MainWindow::deletexml()
{
QFile file("test.xml");
if(!file.open(QFile::ReadOnly))
{
file.close();
return;
}
QDomDocument doc;
if(!doc.setContent(&file))
{
file.close();
return;
}
file.close();
QDomElement root = doc.documentElement();
QDomNodeList nodeList = doc.elementsByTagName("book");
for(int i = 0; i < nodeList.size(); ++i)
{
QDomElement e = nodeList.at(i).toElement();
if(e.attribute("time") == "2019.2.1")
{
root.removeChild(nodeList.at(i));//删除
}
}
if(!file.open(QFile::WriteOnly|QFile::Truncate))
{
return;
}
QTextStream stream(&file);
doc.save(stream,4);
file.close();
}
void MainWindow::modifyxml()
{
QFile file("test.xml");
if(!file.open(QFile::ReadOnly))
{
file.close();
return;
}
QDomDocument doc;
if(!doc.setContent(&file))
{
file.close();
return;
}
file.close();
QDomElement root = doc.documentElement();
QDomNodeList nodeList = root.elementsByTagName("book");
QDomNode node = nodeList.at(nodeList.size() - 1).firstChild();
node.firstChild().setNodeValue("Emma");//修改
if(!file.open(QFile::WriteOnly|QFile::Truncate))
{
return;
}
QTextStream stream(&file);
doc.save(stream,4);
file.close();
}