QNetworkRequest request; request.setUrl(url); request.setRawHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); request.setRawHeader("Content-Type", "text/xml");// networkManager = new QNetworkAccessManager (this); //создаётся в конструкторе QNetworkReply* reply = networkManager->get(request); connect(reply, SIGNAL(readyRead()), this, SLOT(slotReadyRead())); connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(getError(QNetworkReply::NetworkError)));
QNetworkReply *reply=qobject_cast<QNetworkReply*>(sender()); disconnect(reply, SIGNAL(readyRead()), this, SLOT(slotReadyRead())); disconnect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(getError(QNetworkReply::NetworkError))); int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); qDebug() << "================================="; if (reply->error() == QNetworkReply::NoError) { qDebug() << "status code: " << statusCode; switch (statusCode) { case 200: { QByteArray bytes = reply->readAll(); QString XMLstring(bytes); if (!mDocument.setContent(bytes)) { qDebug() << "Error XML"; return ; } QDomElement mElement = mDocument.documentElement().firstChildElement("Product"); while ( !mElement.isNull() ) { QDomElement de = mElement.firstChildElement("ProductName"); productsList << de.text(); mElement = mElement.nextSiblingElement("Product"); } } } }
C++ (Qt)bool QDomDocument::setContent ( const QByteArray & buffer, QString * errorMsg = 0, int * errorLine = 0, int * errorColumn = 0 )
C++ (Qt)bool QDomDocument::setContent ( const QString & text, QString * errorMsg = 0, int * errorLine = 0, int * errorColumn = 0 )
qDebug() << "status code: " << statusCode; ................ QDomDocument mDocument; bool namespaceProcessing; QString * errorMsg; int * errorLine; int * errorColumn; if (!mDocument.setContent(bytes, namespaceProcessing, errorMsg, errorLine, errorColumn)) { qDebug() << "Error XML"; qDebug() << errorMsg; qDebug() << errorLine; qDebug() << errorColumn; return ; }