直接拷贝肯定是跑不通的,此处只介绍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文件如下:
<?xml version="1.0" encoding="utf-8"?>
<QtSoap>
<Host>192.168.8.100</Host>
<Port>80</Port>
<Post>/js45th/JDVN/JDVNExtendService/DVNExtendServiceTH.asmx</Post>
<Space>http://JetsenNet/JDVN/</Space>
<Action>
<Method Id="No1">getBlurayTaskList</Method>
<Method Id="No2">pubCallWebService</Method>
<Method Id="No3">updateBlurayTask</Method>
</Action>
</QtSoap>
const QString QTSOAP_XML_DIR = "\\xml\\";
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";
QString m_HostName;
QString m_HostPort;
#include <QtCore/QtCore>
#include <string>
#include <QtXml/QDomElement>
{
QString vPath = QCoreApplication::applicationDirPath();
vPath.replace(QString("/"),QString("\\"));;
QString vXmlFileName = vPath + QTSOAP_XML_DIR + "BlurayTask.xml";
if (!vXmlFile.open(QIODevice::ReadOnly | QFile::Text))
{
qDebug() << vXmlFileName;
vXmlFile.close();
return;
}
QString vErrStr;
int vErrLine;
int vErrColumn;
if (!vXmlDoc.setContent(&vXmlFile, false, &vErrStr, &vErrLine, &vErrColumn))
{
qDebug() << vErrStr;
vXmlFile.close();
return;
}
vXmlFile.close();
if (QString::compare(vXmlRoot.tagName(), QTSOAP_XML_ROOT, Qt::CaseInsensitive) != 0)
{
qDebug() << "XmlRoot : " + vXmlRoot.tagName();
return;
}
else
{
WriteXmlRootElement(vXmlDoc, vXmlRoot);
}
if (!f.open(QFile::WriteOnly | QFile::Text))
{
f.close();
return;
}
QTextStream out(&f);
vXmlDoc.save(out, 4);
f.close();
}
{
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)
{
QDomElement vNewElement = aXmlDoc.createElement(vtagName);
QDomText vValueText = aXmlDoc.createTextNode(m_HostName);
vNewElement.appendChild(vValueText);
element.replaceChild(vNewElement, vChild);
vChild = vNewElement;
}
else if (QString::compare(vtagName, QTSOAP_XML_HOST_PORT, Qt::CaseInsensitive) == 0)
{
QDomElement vNewElement = aXmlDoc.createElement(vtagName);
QDomText vValueText = aXmlDoc.createTextNode(m_HostPort);
vNewElement.appendChild(vValueText);
element.replaceChild(vNewElement, vChild);
vChild = vNewElement;
}
vChild = vChild.nextSibling();
}
}