void ServerWindow::slotDataSend(QString dataToSend){ QByteArray messageForSend; QDataStream sendStream(& messageForSend, QIODevice::ReadWrite); sendStream.setVersion(QDataStream::Qt_DefaultCompiledVersion); sendStream << quint16(0) << dataToSend; sendStream.device()->seek(0); sendStream << (quint16)(messageForSend.size() - sizeof(quint16)); tcpConnection->write(messageForSend);}
void MainWindow::readResponse(){ static quint16 nextBlockSize = 0; QByteArray tmpData; QDataStream stream(&MySocket::tcpSocket); stream.setVersion(QDataStream::Qt_DefaultCompiledVersion); while(true) { if(nextBlockSize == 0) { if(MySocket::tcpSocket.bytesAvailable() < sizeof(quint16)) break; stream >> nextBlockSize; } if(MySocket::tcpSocket.bytesAvailable() < nextBlockSize) break; while(!stream.atEnd()) { stream >> tmpData; } nextBlockSize = 0; } QDomDocument domDoc; domDoc.setContent(tmpData); QDomElement domElem = domDoc.documentElement(); parseResponseType(domElem);}
C++ (Qt) while(!stream.atEnd()) { stream >> tmpData; }
C++ (Qt) sendStream << quint16(0) << dataToSend; // QString! sendStream.device()->seek(0); sendStream << (quint16)(messageForSend.size() - sizeof(quint16));
C++ (Qt)tcpConnection->write(messageForSend);
C++ (Qt)stream >> nextBlockSize;
C++ (Qt)stream >> tmpData; // QByteArray!
C++ (Qt)QByteArray slotDataSend( const QString & dataToSend ){ QByteArray messageForSend; QDataStream sendStream( &messageForSend, QIODevice::ReadWrite ); //sendStream.setVersion( QDataStream::Qt_DefaultCompiledVersion ); sendStream << quint16(0) << dataToSend; sendStream.device()->seek(0); sendStream << quint16( messageForSend.size() - sizeof( quint16 ) ); return messageForSend;} QString readResponse ( const QByteArray & array ){ static quint16 nextBlockSize = 0; QString tmpData; QDataStream stream( array ); //stream.setVersion( QDataStream::Qt_DefaultCompiledVersion ); while(true) { if ( nextBlockSize == 0 ) { if ( stream.device()->bytesAvailable() < sizeof(quint16) ) break; stream >> nextBlockSize; } if ( stream.device()->bytesAvailable() < nextBlockSize ) break; stream >> tmpData; nextBlockSize = 0; } return tmpData;} int main(int argc, char *argv[]){ QCoreApplication a( argc, argv ); QString text = QString::fromUtf8( "Привет, Русский язык!" ); QString result = readResponse( slotDataSend( text ) ); qDebug() << QString::fromUtf8( "Исходный текст:" ) << text; qDebug() << QString::fromUtf8( "Результат:" ) << result; return a.exec();}
<?xml version="1.0" encoding="UTF-8"?><championship> <teams> <team name="Torpeda" course="45" totalScoring="14" totalMissing="4"> <players> <player num="1" otch="Vital`evich" name="Ivan" fam="Kalashnikov"/> <player num="2" otch="Popovich" name="Oleg" fam="Andreev"/> <player num="3" otch="Ivanovich" name="Kirill" fam="Afanosov"/> <player num="4" otch="Olegovich" name="Petr" fam="Igorkevich"/> <player num="5" otch="Petrovich" name="Alex" fam="Voron"/> <player num="6" otch="Alexseevich" name="Andrey" fam="Dronov"/> </players> </team> </teams></championship>
GetChampionshipData chData;chData.traverseNode();void GetChampionshipData::traverseNode(){ QFile file("C://Users//User//Desktop//work//serrv_v3.2//DATA.xml"); if(!file.open(QIODevice::ReadOnly)) { QMessageBox::warning(0, "Ошибка", "Не удалось найти DATA.xml", QMessageBox::Ok); } else { QDomDocument domDoc; domDoc.clear(); domDoc.setContent(&file); QDomElement domElement = domDoc.documentElement(); traverseTeamNode(domElement); } file.close();}void GetChampionshipData::traverseTeamNode(const QDomNode &node){ QDomElement teamsNode = node.firstChildElement(); QDomElement teamNode = teamsNode.firstChildElement(); while(!teamNode.isNull()) { if(teamsNode.tagName() == "teams") { if(teamNode.tagName() == "team") { Team tm; tm.loadFromNode(teamNode); teams.push_back(tm); } teamNode = teamNode.nextSiblingElement(); } }}void Team::loadFromNode(QDomElement &teamNode){ if(!teamNode.attribute("name").isEmpty()) name = teamNode.attribute("name"); // здесь читаются кракозябры if(!teamNode.attribute("totalScoring").isEmpty()) scoringGoal = teamNode.attribute("totalScoring").toInt(); if(!teamNode.attribute("totalMissing").isEmpty()) missingGoal = teamNode.attribute("totalMissing").toInt(); QDomElement teamPlayers = teamNode.firstChildElement(); QDomElement teamPlayer = teamPlayers.firstChildElement(); while(!teamPlayers.isNull()) { if(!teamPlayer.isNull()) { if(teamPlayer.tagName() == "player") { Player player; player.loadFromNode(teamPlayer); players.push_back(player); } teamPlayer = teamPlayer.nextSiblingElement(); } else break; }}void Player::loadFromNode(const QDomElement &playerNode){ if(!playerNode.attribute("num").isEmpty()) num = playerNode.attribute("num"); if(!playerNode.attribute("otch").isEmpty()) otch = playerNode.attribute("otch"); // здесь читаются кракозябры if(!playerNode.attribute("name").isEmpty()) name = playerNode.attribute("name"); // здесь читаются кракозябры if(!playerNode.attribute("fam").isEmpty()) fam = playerNode.attribute("fam"); // здесь читаются кракозябры if(!playerNode.attribute("goal").isEmpty()) goal = playerNode.attribute("goal");}