You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

188 lines
5.4 KiB

#include "XMLReader.h"
/*****************************************************************************/
/**
* @brief Constructor
* @details
*
* @return Nothing
*/
/*****************************************************************************/
XMLReader::XMLReader(QString xmlAddress)
{
QFile xmlFile(xmlAddress);
if(!xmlFile.open(QIODevice::ReadOnly))
{
//"XML file not find"
}
if (!_xmlDoc.setContent(&xmlFile))
{
// "Oops!!, The file is not a xml file."
}
xmlFile.close();
getDatabaseNameList();
}
/*****************************************************************************/
/**
* @brief Get a list of databases's Names
* @details
*
* @return QVector<QString> DBNames
*/
/*****************************************************************************/
QVector<QString> XMLReader::getDatabaseNameList()
{
_DBElementVector.clear();
_root = _xmlDoc.firstChildElement();
QDomNodeList DBs = _root.elementsByTagName("Database");
for (int DBIndex = 0; DBIndex < DBs.count(); ++DBIndex )
{
QDomElement DBElement = DBs.at(DBIndex).toElement();
_DBNameVector.append(DBElement.attribute("name"));
_DBElementVector.append(DBElement);
}
return _DBNameVector;
}
/*****************************************************************************/
/**
* @brief Method to get a list of a database's tables
* @details
*
* @return QVector<QString> tblNmaes
*/
/*****************************************************************************/
QVector<QString> XMLReader::getDBTableList(QString DBName)
{
_tableNamevector.clear();
bool wasDB = false;
for (int i = 0; i < _DBElementVector.count(); ++i)
{
if(_DBElementVector.at(i).attribute("name") == DBName)
{
wasDB = true;
if(!_DBElementVector.at(i).isElement())
{
QString logText = "Oops!!, The xml file is incorrect. Error was at : " +
_DBElementVector.at(i).text();
//Erorr logText
break;
}
QDomNodeList tblNodes = _DBElementVector.at(i).elementsByTagName("Table");
for (int tindex = 0; tindex < tblNodes.count(); ++tindex)
{
QDomElement tableElement = tblNodes.at(tindex).toElement();
_tableElementVector.append(tableElement);
_tableNamevector.append(tableElement.attribute("name"));
}
break;
}
}
if(!wasDB)
{
QString logText = "Oops!!, The xml file does not have Database Tage by name : " +
DBName;
//Erorr logText
return _tableNamevector ;
}
if( _tableNamevector.count() == 0)
{
QString logText = "Oops!!, The " +
DBName +
"exist but does not have any table";
//Erorr logText
return _tableNamevector ;
}
return _tableNamevector ;
}
/*****************************************************************************/
/**
* @brief Method to get a list of a database's tables
* @details
*
* @return QVector<QString> tblNmaes
*/
/*****************************************************************************/
QVector<QString> XMLReader::getDBTableList(QDomElement DBElement)
{
_tableNamevector.clear();
QDomNodeList tblNodes = DBElement.elementsByTagName("Table");
for (int tblIndex = 0; tblIndex < tblNodes.count(); ++tblIndex)
{
if(!tblNodes.at(tblIndex).isElement())
{
QString logText = "Oops!!, The xml Database Tage by name : " +
DBElement.attribute("name") +
" is not xml element";
//Error logText
break;
}
QDomElement tableElement = tblNodes.at(tblIndex).toElement();
_tableNamevector.append(tableElement.attribute("name"));
}
if( _tableNamevector.count() == 0)
{
QString logText = "Oops!!, The " +
DBElement.attribute("name") +
"exist but does not have any table";
//Error logText
}
return _tableNamevector;
}
QVector<QDomElement> XMLReader::getLastFieldElements()
{
return _fieldElementVector;
}
QVector<QDomElement> XMLReader::getFieldElements(QDomElement tableElement)
{
_fieldElementVector.clear();
QDomNodeList fieldNodes = tableElement.elementsByTagName("Field");
if(fieldNodes .count() > 0)
{
for(int i = 0; i < fieldNodes.count(); i++)
{
_fieldElementVector.append(fieldNodes.at(i).toElement());
}
}
else
{
//Error there is no Field.
}
return _fieldElementVector;
}
QVector<QDomElement> XMLReader::getFieldElements(QString tableName)
{
_fieldElementVector.clear();
if(_tableElementVector.count() > 0)
{
for(int j = 0; j < _tableElementVector.count(); j++)
{
if(_tableElementVector.at(j).attribute("name") == tableName)
{
QDomNodeList fieldNodes = _tableElementVector.at(j).elementsByTagName("Field");
if(fieldNodes .count() > 0)
{
for(int i = 0; i < fieldNodes.count(); i++)
{
_fieldElementVector.append(fieldNodes.at(i).toElement());
}
}
else
{
//Error there is no Field.
}
break;
}
}
}
return _fieldElementVector;
}