QT xml文件转化lua table文件 工具

来源:互联网 发布:shake it off 下载 编辑:程序博客网 时间:2024/05/02 05:01



经过两天的奋战,xml 文件 转化为lua table 的工具终于搞成了。
先上代码:

xmltoluatool.h

#ifndef XMLTOLUATOOL_H#define XMLTOLUATOOL_H#include <QtWidgets/QMainWindow>#include "qdom.h"#include "ui_xmltoluatool.h"class XmlToLuaTool : public QMainWindow{Q_OBJECTpublic:XmlToLuaTool(QWidget *parent = 0);~XmlToLuaTool();private:Ui::XmlToLuaToolClass ui;QString inPath;QString outPath;QList<QFileInfo> *xmlFileInfo;QFile *sourcefile;QString sourceData;QString desData;QString desTableName;QString log;bool result;private slots:void selectXmlDir();void selectluaDir();bool makeLuaFiles();private:void init();bool XmlToLuaTool::parseXml(QString file_name);QString makeLuaString(QString str);bool writeContentToFile(QString str,QString fileName);};#endif // XMLTOLUATOOL_H

xmltoluatool.cpp
#include "stdafx.h"#include "xmltoluatool.h"XmlToLuaTool::XmlToLuaTool(QWidget *parent): QMainWindow(parent){ui.setupUi(this);sourceData = "";connect(ui.SelectXmlDirButton,SIGNAL(pressed()),this,SLOT(selectXmlDir()));connect(ui.SelectLuaDirButton,SIGNAL(pressed()),this,SLOT(selectluaDir()));connect(ui.OutputButton,SIGNAL(pressed()),this,SLOT(makeLuaFiles()));}XmlToLuaTool::~XmlToLuaTool(){if(xmlFileInfo){xmlFileInfo = NULL;delete xmlFileInfo;}}void XmlToLuaTool::selectXmlDir(){inPath=QFileDialog::getExistingDirectory(NULL, NULL,"D:\\",QFileDialog::ShowDirsOnly);if(inPath.isEmpty()){QMessageBox::information(NULL, QString("title"), QString("please select xml folder!"));return;  }ui.XmlDirPath->setText(inPath);ui.XmlDirPath->show();}void XmlToLuaTool::selectluaDir(){if(inPath.isEmpty()){QMessageBox::information(NULL, QString("title"), QString("please select xml folder!"));return;  }outPath=QFileDialog::getExistingDirectory(NULL, NULL,"D:\\",QFileDialog::ShowDirsOnly);if(outPath.isEmpty()){QMessageBox::information(NULL, QString("title"), QString("please select out folder!"));return;  }ui.LuaDirPath->setText(outPath);ui.LuaDirPath->show();}bool XmlToLuaTool::makeLuaFiles(){QDateTime time_ = QDateTime::currentDateTime();//获取系统现在的时间QString str_ = time_.toString("yyyy-MM-dd hh:mm:ss ddd"); //设置显示格式qDebug()<<str_;if(outPath.isEmpty()){QMessageBox::information(NULL, QString("title"), QString("please select folder!"));return false;  }//判断路径是否存在QDir dir(inPath);if(!dir.exists()){return false;}//获取所选文件类型过滤器QStringList filters;filters<<QString("*.xml");//定义迭代器并设置过滤器QDirIterator dir_iterator(inPath,filters,QDir::Files | QDir::NoSymLinks,QDirIterator::Subdirectories);//循环操作文件while(dir_iterator.hasNext()){//初始化init();dir_iterator.next();QFileInfo file_info = dir_iterator.fileInfo();QString absolute_file_path = file_info.absoluteFilePath();if (this->parseXml(absolute_file_path)){if(writeContentToFile(desData,desTableName)){log.append(desTableName);log.append(".lua make success!\r");}else{QString tmp = desTableName;tmp.append(".lua make fail!\r");log.append(tmp);}}}//记录日志ui.LogBrower->setText(log);ui.LogBrower->show();QDateTime time = QDateTime::currentDateTime();//获取系统现在的时间QString str = time.toString("yyyy-MM-dd hh:mm:ss ddd"); //设置显示格式qDebug()<<str;}bool XmlToLuaTool::parseXml(QString file_name){QFile file(file_name);  if(!file.open(QFile::ReadOnly | QFile::Text)){  QMessageBox::information(NULL, QString("title"), QString("open error!"));return false;  }  QXmlStreamReader reader;// 设置文件,这时会将流设置为初始状态reader.setDevice(&file);//拼接table头int startIndex = file_name.lastIndexOf("/");QString  tableName = file_name.mid(startIndex+1);tableName = tableName.mid(0,tableName.lastIndexOf(".")-1);desTableName = tableName;tableName.append("=\r{");QString tmp = "t_";//由于lua table 不支持纯数字命名makeLuaString(tmp.append(tableName));// 如果没有读到文档结尾,而且没有出现错误while (!reader.atEnd()) {// 读取下一个记号,它返回记号的类型QXmlStreamReader::TokenType type = reader.readNext();// 下面便根据记号的类型来进行不同的输出if (type == QXmlStreamReader::StartDocument){qDebug() << reader.documentEncoding() << reader.documentVersion();}if (type == QXmlStreamReader::StartElement){QString _name = " ";_name.append(reader.name());_name.append("={");makeLuaString(_name);QXmlStreamAttributes attributes = reader.attributes();if (attributes.count() > 0){for (int i = 0;i<attributes.size();i++){QXmlStreamAttribute attribute = attributes.at(i);QString attributeName = " ";attributeName.append(attribute.name());attributeName.append("=\"");attributeName.append(attribute.value());attributeName.append("\",");makeLuaString(attributeName);}}}if (type == QXmlStreamReader::Characters||QXmlStreamReader::Comment){QString tmp = "";tmp.append(reader.text());makeLuaString(tmp);}if (type == QXmlStreamReader::EndElement){makeLuaString("},");}}makeLuaString("\r}");// 如果读取过程中出现错误,那么输出错误信息if (reader.hasError()) {qDebug() << "error: " << reader.errorString();return false;}file.close();//qDebug() << desData; return true;}QString XmlToLuaTool::makeLuaString(QString str){//qDebug()<<str;desData.append(str);return desData;}bool XmlToLuaTool::writeContentToFile(QString str,QString fileName_){if(str.isEmpty()){QMessageBox::information(NULL, QString("title"), QString("the sourceData is empty!"));return false;}QString fileName = outPath;fileName.append("/");fileName.append(fileName_);fileName.append(".lua");QFile file(fileName); if(!file.open(QIODevice::WriteOnly | QIODevice::Text))  {  QMessageBox::warning(this,"","can't open",QMessageBox::Yes);  return false;}  QTextStream in(&file);in<<str;//如果写多行,同上循环即可  file.close();return true;}void XmlToLuaTool::init(){sourcefile = nullptr;sourceData = "";desData = "";desTableName = "";result = false;}
代码已经测试通过,用QT 做的UI,有不对的地方请大家多多指教。

下载:http://download.csdn.net/detail/langzi007008/8266323

0 0
原创粉丝点击