直接拷贝肯定是跑不通的,此处只介绍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文件如下:
192.168.8.100
80
/js45th/JDVN/JDVNExtendService/DVNExtendServiceTH.asmx
getBlurayTaskList
pubCallWebService
updateBlurayTask
代码:
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";
private:
QString m_HostName;
QString m_HostPort;
#include
#include
#include
void CVxWebServiceHost::SaveWebServicesXml()
{
QString vPath = QCoreApplication::applicationDirPath();
vPath.replace(QString("/"),QString("\\"));;
QString vXmlFileName = vPath + QTSOAP_XML_DIR + "BlurayTask.xml";
QFile vXmlFile(vXmlFileName);
if (!vXmlFile.open(QIODevice::ReadOnly | QFile::Text))
{
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
{
WriteXmlRootElement(vXmlDoc, vXmlRoot);
}
QFile f(vXmlFileName);
if (!f.open(QFile::WriteOnly | QFile::Text))
{
f.close();
return;
}
QTextStream out(&f);
vXmlDoc.save(out, 4);
f.close();
}
void CVxWebServiceHost::WriteXmlRootElement(QDomDocument &aXmlDoc, 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)
{
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();
}
}
值得注意的就是不能使用setNodeValue()方法来修改接点的值,经过测试不起作用,本文使用的接点替换方法.
阅读(12181) | 评论(0) | 转发(0) |