XML文件的增、删、改、查
增
void MainWindow::onBtnAddClicked()
{
QString filePath = QCoreApplication::applicationDirPath() + "/hh.xml";
QFile file(filePath);
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(QString("book"));
book.setAttribute(QString("id"),3);
QDomElement author = doc.createElement(QString("author"));
QDomText text = doc.createTextNode(QStringLiteral("张三"));
author.appendChild(text);
book.appendChild(author);
QDomElement name = doc.createElement(QString("bookName"));
text = doc.createTextNode(QStringLiteral("《活着》"));
name.appendChild(text);
book.appendChild(name);
QDomElement price = doc.createElement(QString("price"));
price.setAttribute(QString("money"),35.6);
book.appendChild(price);
QDomElement num = doc.createElement(QString("count"));
text = doc.createTextNode(QString("666"));
num.appendChild(text);
book.appendChild(num);
root.appendChild(book);
if(!file.open(QFile::WriteOnly|QFile::Truncate)){
return;
}
QTextStream stream(&file);
doc.save(stream,4);
file.close();
}
删
void MainWindow::onBtnDeteteClicked()
{
QString filePath = QCoreApplication::applicationDirPath() + "/hh.xml";
QFile file(filePath);
qDebug()<< filePath;
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(QString("book"));
for(int i=0; i<list.size(); i++){
QDomElement element = list.at(i).toElement();
if(element.attribute(QString("id")) == QString("1")){
root.removeChild(list.at(i));
}
}
if(!file.open(QFile::WriteOnly| QFile::Truncate)){
return;
}
QTextStream stream(&file);
doc.save(stream,4);
file.close();
return ;
}
改
void MainWindow::onBtnUpdateClicked()
{
QString filePath = QCoreApplication::applicationDirPath() + "/hh.xml";
QFile file(filePath);
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(QString("book"));
QDomNode node = list.at(list.size()-1).firstChild();
QDomNode oldNode = node.firstChild();
node.firstChild().setNodeValue(QStringLiteral("曹雪芹"));
QDomNode newNode = node.firstChild();
node.replaceChild(newNode,oldNode);
QDomNode nextNode = node.nextSibling();
nextNode.firstChild().setNodeValue(QStringLiteral("《红楼梦》"));
nextNode = nextNode.nextSibling();
nextNode = nextNode.nextSibling();
nextNode.firstChild().setNodeValue(QString("900"));
if(!file.open(QFile::WriteOnly|QFile::Truncate)){
return;
}
QTextStream stream(&file);
doc.save(stream,4);
file.close();
return ;
}
查
void MainWindow::onBtnReadClicked()
{
QString filePath = QCoreApplication::applicationDirPath();
filePath.append(tr("/hh.xml"));
QFile file(filePath);
if(!file.open(QFile::ReadOnly)){
return;
}
QDomDocument doc;
if(!doc.setContent(&file)){
file.close();
return;
}
file.close();
ui->tableWidget->setColumnCount(5);
ui->tableWidget->setHorizontalHeaderLabels(header);
int row = 0;
QDomElement root = doc.documentElement();
QDomNode node = root.firstChild();
while (!node.isNull()) {
if(node.isElement()){
ui->tableWidget->setRowCount(row+1);
QDomElement element = node.toElement();
QDomNodeList list = element.childNodes();
qDebug()<< element.tagName() << element.attribute("id");
ui->tableWidget->setItem(row,0,new QTableWidgetItem(element.attribute("id")));
for(int i=0; i<list.size();i++){
QDomNode n = list.at(i);
if(node.isElement()){
qDebug() << n.nodeName() << ": " << n.toElement().text();
ui->tableWidget->setItem(row,i+1,new QTableWidgetItem(n.toElement().text()));
}
}
row++;
}
node = node.nextSibling();
}
}
创建xml文件
void MainWindow::onBtnCreateClicked()
{
QString filePath = QCoreApplication::applicationDirPath();
filePath.append(tr("/hh.xml"));
QFile file(filePath);
if(!file.open(QFile::WriteOnly|QFile::Truncate)){
return;
}
ui->tableWidget->clear();
ui->tableWidget->setRowCount(0);
ui->tableWidget->setHorizontalHeaderLabels(header);
QDomDocument doc;
QDomProcessingInstruction instruct = doc.createProcessingInstruction(QString("xml"),QString("version =\"1.0\" encoding=\"UTF-8\""));
doc.appendChild(instruct);
QDomElement root = doc.createElement(QString("library"));
doc.appendChild(root);
QDomElement book = doc.createElement(QString("book"));
book.setAttribute(QString("id"),1);
QDomElement author = doc.createElement(QString("author"));
QDomText text = doc.createTextNode(QString("LHG"));
author.appendChild(text);
book.appendChild(author);
QDomElement name = doc.createElement(QString("bookName"));
text = doc.createTextNode(QString("C++"));
name.appendChild(text);
book.appendChild(name);
QDomElement price = doc.createElement(QString("price"));
price.setAttribute(QString("money"),33.6);
book.appendChild(price);
QDomElement num = doc.createElement(QString("count"));
text = doc.createTextNode(QString("100"));
num.appendChild(text);
book.appendChild(num);
root.appendChild(book);
book = doc.createElement(QString("book"));
book.setAttribute(QString("id"),2);
author = doc.createElement(QString("author"));
text = doc.createTextNode(QString("GuiGe"));
author.appendChild(text);
book.appendChild(author);
name = doc.createElement(QString("bookName"));
text = doc.createTextNode(QStringLiteral("三国演义"));
name.appendChild(text);
book.appendChild(name);
price = doc.createElement(QString("price"));
price.setAttribute(QString("money"),156.6);
book.appendChild(price);
num = doc.createElement(QString("count"));
text = doc.createTextNode(QString("200"));
num.appendChild(text);
book.appendChild(num);
root.appendChild(book);
QTextStream stream(&file);
doc.save(stream,4);
file.close();
}
设置按钮按下不同颜色
ui->pushButton-> setStyleSheet("QPushButton{background-color:rgb(19, 100, 181);color: white; } QPushButton:pressed{background-color:rgb(134, 134, 134)}");