首页 > 学院 > 开发设计 > 正文

大三软件工程小项目-小技术集合-读取XML文件及运行外部程序

2019-11-06 07:07:50
字体:
来源:转载
供稿:网友

服务端源码下载地址为:

http://download.csdn.net/detail/QQ78442761/9768662

关于什么是xml文件,在此不在啰嗦。

不知道的博友请百度补脑。

本程序的xml在如下地方:

xml内容如下:

在本程序中,此xml存储了外部程序路径,用于打开外部程序的。如下图所示:

单击了运行MySQL后就会运行外部程序如下图所示:

另外一个外部程序不做演示。

我们现在来讲解一个简单的,如何进行外部程序。

我们在MainWindow.里面看到关于这个按钮的函数,如下

//运行外部MYSQL程序void MainWindow::RunMYSQLPRocess(){    QString cmd=QString(qPrintable(ProXML->GetProcessDir("MYSQL")));    m_Process.start(cmd,QStringList()<<"");    ui->CMDplainTextEdit->appendPlainText(CurrTime::currentDateTime()+"打开外部MYSQL程序");}我们现在来定位m_Process。

我们这mainwindow.h里面可以看见一个私有的成员变量。

    QProcess m_Process;     //打开外部程序这个QProcess是在头文件

#include <QProcess>有这个start函数,就可以对外部程序进行激活。

下面是读取xml。

在这个项目里面,有一个docxml.h和docxml.cpp用于读取xml

最上面的那个链接可以下载全套源码,IDE为 Qt Creator 5.7

docxml.h

#ifndef DOCXML_H#define DOCXML_H#include <QtXml>#include <QDomDocument>#include <QFile>#include <QIODevice>#include <QDomNode>#include <QDebug>#include <QMessageBox>#include <QString>class DOCXML{public:    DOCXML();    QString GetProcessDir(QString ProName); //获取程序路径private:    QDomDocument *doc;   //QDomDocument类对象,代表一个XML文档    QFile *file;     //建立指向in.xml文件的QFile对象    QDomNode *Node;  //XML结点};#endif // DOCXML_H

docxml.cpp

#include "docxml.h"DOCXML::DOCXML(){   file=new QFile("in.xml");   if(!file->open(QIODevice::ReadOnly))   {       QMessageBox::critical(NULL,"错误","in.xml打开失败",QMessageBox::Cancel);   }   else   {       //将文件读入doc中       doc=new QDomDocument();       if(!doc->setContent(file))       {           QMessageBox::critical(NULL,"错误","in.xml读取",QMessageBox::Cancel);       }       else           file->close();   }}QString DOCXML::GetProcessDir(QString ProName){    QDomElement docElem = doc->documentElement();  //返回根元素    QDomNode n = docElem.firstChild();   //返回根节点的第一个子节点    //如果节点不为空    while(!n.isNull())    {        if (n.isElement())  //如果节点是元素        {            QDomElement e = n.toElement();            // 获得元素e的子节点的列表            QDomNodeList list = e.childNodes();            //获取程序名            QDomNode node = list.at(0);            if(ProName==qPrintable(node.toElement().text()))            {                node = list.at(1);                //qDebug() << qPrintable(node.toElement().text());;                return qPrintable(node.toElement().text());            }        }          n = n.nextSibling();  //下一个兄弟节点    }    return "fail";}这就是读取XML文件及运行外部程序


上一篇:搜索树判断

下一篇:内部类

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表