分类: C/C++
2011-05-20 16:57:40
直接拷贝肯定是跑不通的,此处只介绍QDomDocument读xml的方法.
使用QDomDocument需要在 xxx.pro 中添加: qt += xml
没有xxx.pro 文件怎么办呢?
方法一: "开始" → "运行(R)" : cmd → "切换目录到工程目录" → 输入: "qmake -project" 回车运行将会生成.pro文件, 采用记事本就可以打开.pro文件.
方法二: "所有程序" → "Microsoft Visual Studio 2010(或:Microsoft Visual Studio 2005)" → "Visual Studio Tools" → "Visual Studio 命令提示(2010)(或:Visual Studio 命令提示(2005))" → "切换目录到工程目录" → 输入: "qmake -project" 回车运行将会生成.pro文件
设置项目属性:
"配置属性" → "C/C++" → "常规" → "附加包含目录" $(QTDIR)\include\QtXml
"配置属性" → "链接器" → "输入" → "附加依赖项" QtXmld4.lib(QtXml4.lib)
本文所使用的XML文件如下:
代码:
const QString QTSOAP_XML_DIR = "";
const QString QTSOAP_XML_ROOT = "Qtsoap";
const QString QTSOAP_XML_HOST_NAME = "Host";
const QString QTSOAP_XML_NAME_SPACE = "Space";
const QString QTSOAP_XML_HOST_PORT = "Port";
const QString QTSOAP_XML_HOST_POST = "Post";
const QString QTSOAP_XML_ACTION_NAME = "Action";
const QString QTSOAP_XML_METHOD_NO = "Id";
const QString QTSOAP_XML_METHOD_NAME = "Method";
class CVxQtSoap : public CVxBaseQtSoap
{
Q_OBJECT
private:
QString m_HostName;
QString m_NameSpace;
quint16 m_HostPort;
QString m_HostPost;
QString m_MethodNo;
QString m_MethodName;
};
#include
#include
#include
void CVxQtSoap::SetWebServicesXml(const QString &aXmlName, const QString &aMethodNo)
{
m_MethodNo = aMethodNo;
QString vPath = QCoreApplication::applicationDirPath();
vPath.replace(QString("/"),QString("\\"));;
QString vXmlFileName = vPath + QTSOAP_XML_DIR + aXmlName;
QFile vXmlFile(vXmlFileName);
if (!vXmlFile.open(QIODevice::ReadOnly))
{
qDebug() << vXmlFileName;
vXmlFile.close();
return;
}
QDomDocument vXmlDoc;
QString vErrStr;
int vErrLine;
int vErrColumn;
if (!vXmlDoc.setContent(&vXmlFile, false, &vErrStr, &vErrLine, &vErrColumn))
{
qDebug() << vErrStr;
vXmlFile.close();
return;
}
vXmlFile.close();
QDomElement vXmlRoot = vXmlDoc.documentElement();
if (QString::compare(vXmlRoot.tagName(), QTSOAP_XML_ROOT, Qt::CaseInsensitive) != 0)
{
qDebug() << "XmlRoot : " + vXmlRoot.tagName();
return;
}
else
{
ParesXmlRootElement(vXmlRoot);
}
}
void CVxQtSoap::ParesXmlRootElement(const QDomElement &element)
{
QDomNode vChild = element.firstChild();
while (!vChild.isNull())
{
QDomElement vElement;
vElement = vChild.toElement();
QString vtagName = vElement.tagName();
if (QString::compare(vtagName, QTSOAP_XML_HOST_NAME, Qt::CaseInsensitive) == 0)
{
m_HostName = vElement.text();
}
else if (QString::compare(vtagName, QTSOAP_XML_NAME_SPACE, Qt::CaseInsensitive) == 0)
{
m_NameSpace = vElement.text();
}
else if (QString::compare(vtagName, QTSOAP_XML_HOST_PORT, Qt::CaseInsensitive) == 0)
{
QString vPort = vElement.text();
bool vIsOk;
int vPortInt = vPort.toUInt(&vIsOk, 10);
m_HostPort = vPortInt;
}
else if (QString::compare(vtagName, QTSOAP_XML_HOST_POST, Qt::CaseInsensitive) == 0)
{
m_HostPost = vElement.text();
}
else if (QString::compare(vtagName, QTSOAP_XML_ACTION_NAME, Qt::CaseInsensitive) == 0)
{
ParesActionElement(vElement);
}
vChild = vChild.nextSibling();
}
}
void CVxQtSoap::ParesActionElement(const QDomElement &element)
{
QDomNode vChild = element.firstChild();
while (!vChild.isNull())
{
QDomElement vElement;
vElement = vChild.toElement();
QString vtagName = vElement.tagName();
if (QString::compare(vtagName, QTSOAP_XML_METHOD_NAME, Qt::CaseInsensitive) == 0)
{
QString vNo = vElement.attribute(QTSOAP_XML_METHOD_NO);
if (QString::compare(vNo, m_MethodNo, Qt::CaseInsensitive) == 0)
{
m_MethodName = vElement.text();
break;
}
}
vChild = vChild.nextSibling();
}
}