// save.xml
<?xml version="1.0" encoding="UTF-8"?>
<Notes>
<node cxx="cxx">
<no class="class" age="18">001</no>
<name>name1</name>
<sex>female1</sex>
</node>
<node>
<no class="class111" age="18">002</no>
<name>name2</name>
<sex>female2</sex>
</node>
<node>
<no class="class" age="18">003</no>
<name>name3</name>
<sex>female3</sex>
</node>
</Notes>
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QFileDialog>
#include <QFile>
#include <QMessageBox>
#include <QDomDocument>
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_btn_dom_read_clicked();
void on_btn_dom_write_clicked();
void on_pushButton_3_clicked();
void on_pushButton_4_clicked();
private:
Ui::MainWindow *ui;
QFile *myFile;
QString elementName;
void Init();
void domRead();
void domWrite();
void streamRead();
void streamWrite();
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
Init();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::Init(){
myFile = new QFile(this);
}
// Slots
void MainWindow::on_btn_dom_read_clicked()
{
domRead();
}
void MainWindow::on_btn_dom_write_clicked()
{
domWrite();
}
//Methods
// .attribute(), .text()
void MainWindow::domRead(){
QString fileName = QFileDialog::getOpenFileName(this,"getXmlFile",
"../XmlParse/", "xml(*.xml)");
if(fileName.isEmpty()) return;
qDebug() << "filename:" << fileName;
myFile->setFileName(fileName);
bool ret = myFile->open(QIODevice::ReadOnly | QIODevice::Text);
if(!ret){
QMessageBox::warning(this,"warning","Open file failed!");
return;
}
// 对xml文件操作
QDomDocument doc("abc");
ret = doc.setContent(myFile);
if(!ret){ // 文件关联失败
myFile->close();
QMessageBox::warning(this,"warning","xml文件关联失败!");
return;
}
myFile->close();
QDomElement docElem = doc.documentElement();
///qDebug() << docElem.nodeName();
QDomNode n = docElem.firstChild();
// 遍历所有Nodes里面所有的node
while(!n.isNull())
{
//qDebug() << n.nodeName();
QDomElement nElement = n.toElement();
QString str = nElement.attribute("cxx"); //打印node的attribute-cxx
if(!str.isEmpty()) {
qDebug() << str;
}
QDomNodeList childList = n.childNodes();
for(int i=0; i<childList.length(); i++){
QDomNode node = childList.at(i);
//qDebug() << node.nodeName() << "~~" << node.nodeValue();
QDomElement childElement = node.toElement();
if(childElement.isElement()){
if(childElement.nodeName() =="no"){
qDebug() << childElement.text();//001,002
qDebug() << childElement.attribute("age");
qDebug() << childElement.attribute("class");
} else {
qDebug() << childElement.text();
}
}
}//end for
n = n.nextSibling();
}//end while
}
void MainWindow::domWrite(){
QDomDocument doc;
//add instruction
QDomProcessingInstruction ins = doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"UTF-8\"");
doc.appendChild(ins);
//add Notes
QDomElement Notes =doc.createElement("Notes");
doc.appendChild(Notes);
QStringList arrayAge;
arrayAge << "10" << "20"<< "30" << "40" << "50";
QStringList arrayClass;
arrayClass << "math"<< "english" << "sport" << "physic" << "chinese";
QStringList arrayName;
arrayName << "aa"<< "bb"<< "cc"<< "dd"<< "ee";
QStringList arraySex;
arraySex << "M"<< "F"<< "F"<< "MF"<< "M";
QStringList arrayText;
arrayText << "001"<< "002"<< "003"<< "004" << "005";
qDebug() << "build data size:" << arrayAge.size();
//add node1, 遍历1
for(int i=0; i<arrayText.length(); i++){
qDebug() << "index:" << i;
QDomElement node1 = doc.createElement("node");
Notes.appendChild(node1);
//add no1
QDomElement no1 = doc.createElement("no");
node1.appendChild(no1);
//add no1's attribute: class
QDomAttr no1_class1 = doc.createAttribute("class");
// no1_class1.setValue("class010203");
no1_class1.setValue(arrayClass.at(i));
no1.setAttributeNode(no1_class1);
//add no1's attribute: age
QDomAttr no1_age = doc.createAttribute("age");
no1_age.setValue(arrayAge.at(i));
no1.setAttributeNode(no1_age);
//add no1's text
QDomText no1_text1 = doc.createTextNode(arrayText.at(i));
no1.appendChild(no1_text1);
//add name1
QDomElement name1 = doc.createElement("name");
node1.appendChild(name1);
//add name1's text
QDomText name1_text1 = doc.createTextNode(arrayName.at(i));
name1.appendChild(name1_text1);
//add sex
QDomElement sex1 = doc.createElement("sex");
node1.appendChild(sex1);
QDomText sex1_text = doc.createTextNode(arraySex.at(i));
sex1.appendChild(sex1_text);
}
// node2...
QString fileName = QFileDialog::getSaveFileName(this,"saveXmlFile",
"../XmlParse/", "xml(*.xml)");
if(fileName.isEmpty()) return;
qDebug() << "filename:" << fileName;
myFile->setFileName(fileName);
bool ret = myFile->open(QIODevice::WriteOnly | QIODevice::Text);
if(!ret){
QMessageBox::warning(this,"warning","Save xml file failed!");
return;
}
//doc 关联file
QTextStream stream(myFile);
stream.setCodec("utf-8");
doc.save(stream, 4);
myFile->close();
QMessageBox::information(this,"SaveTips","save xml success.");
}
//-----------------------------------------------------
// stream read
void MainWindow::on_pushButton_3_clicked()
{
streamRead();
}
// stream write
void MainWindow::on_pushButton_4_clicked()
{
streamWrite();
}
void MainWindow:: streamRead(){
QString fileName = QFileDialog::getOpenFileName(this,"getXmlFile",
"../XmlParse/", "xml(*.xml)");
if(fileName.isEmpty()) return;
qDebug() << "filename:" << fileName;
myFile->setFileName(fileName);
bool ret = myFile->open(QIODevice::ReadOnly | QIODevice::Text);
if(!ret){
QMessageBox::warning(this,"warning","Open file failed!");
return;
}
// 对xml文件操作
QXmlStreamReader readxml(myFile);
while(!readxml.atEnd()){
if(readxml.hasError()) {
QMessageBox::warning(this,"warning","read xml failed");
return;
}
QXmlStreamReader::TokenType type = readxml.readNext();
switch (type) {
case QXmlStreamReader::ProcessingInstruction:
qDebug() << "ProcessingInstruction:" <<readxml.text();
break;
case QXmlStreamReader::StartElement:
{ // handle attribute
qDebug() << "start element:" << readxml.name().toString();
elementName = readxml.name().toString();
QString class01 = readxml.attributes().value("class").toString();
QString age01 = readxml.attributes().value("age").toString();
if(!class01.isEmpty()) qDebug() << "class" <<class01;
if(!age01.isEmpty()) qDebug() << "age" << age01;
}
break;
case QXmlStreamReader::EndElement:
// qDebug() << "EndElement:";
break;
case QXmlStreamReader::StartDocument:
break;
case QXmlStreamReader::EndDocument:
break;
case QXmlStreamReader::Characters://text
{
QString text01 = readxml.text().toString();
if(!readxml.isWhitespace()) {
if(!elementName.isEmpty()){
qDebug() << "name:" << elementName <<" value:" << text01;
}
}
}
break;
default:
break;
}
}
myFile->close();
}
void MainWindow:: streamWrite(){
QString fileName = QFileDialog::getSaveFileName(this,"saveXmlFile",
"../XmlParse/", "xml(*.xml)");
if(fileName.isEmpty()) return;
qDebug() << "filename:" << fileName;
myFile->setFileName(fileName);
bool ret = myFile->open(QIODevice::WriteOnly | QIODevice::Text);
if(!ret){
QMessageBox::warning(this,"warning","Save xml file failed!");
return;
}
QStringList arrayAge;
arrayAge << "10" << "20"<< "30" << "40" << "50";
QStringList arrayClass;
arrayClass << "math"<< "english" << "sport" << "physic" << "chinese";
QStringList arrayName;
arrayName << "aa"<< "bb"<< "cc"<< "dd"<< "ee";
QStringList arraySex;
arraySex << "M1"<< "F2"<< "F3"<< "MF4"<< "M5";
QStringList arrayText;
arrayText << "001"<< "002"<< "003"<< "004" << "005";
QXmlStreamWriter stream(myFile);
stream.setAutoFormatting(true);
stream.writeStartDocument();
for(int i=0; i<arrayAge.length(); i++){
// add Notes
stream.writeStartElement("Notes");
// add node1
stream.writeStartElement("node");
// add no1
stream.writeStartElement("no");
//add attribute
stream.writeAttribute("class", arrayClass.at(i));
stream.writeAttribute("age", arrayAge.at(i));
stream.writeCharacters(arrayText.at(i));
stream.writeEndElement();
// add name
stream.writeStartElement("name");
stream.writeCharacters(arrayName.at(i));
stream.writeEndElement();
// add sex.
stream.writeStartElement("sex");
stream.writeCharacters(arraySex.at(i));
stream.writeEndElement();
stream.writeEndElement();
stream.writeEndElement();
}//end for
stream.writeEndDocument();
myFile->close();
QMessageBox::information(this,"SaveTips","save xml success.");
}