开发者

XML file from internet and Qt

I want to parse some XML-file in Qt, but that file is located at some server in web. When I u开发者_如何学JAVAsed QML I was able to use XMLHttpRequest class which takes address of file in internet (what I do need).

I have only one idea: use network request and download that xml directly. The idea is maybe there is a special interface in XML parser in qt which takes xml-path from internet?


As I know, you should download it. QHttp provides easy way to download it to a temporary file.

QTemporaryFile temp_file;
QHttp http("example.com");
http.get("/your.xml",&temp_file);

New version (based on QNetworkAccessManager):

QNetworkAccessManager * manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(fileIsReady(QNetworkReply*)) );
manager->get(QNetworkRequest(QUrl("http://example.com/your.xml")));
...
void fileIsReady( QNetworkReply * reply)
{
  QTemporaryFile temp_file;
  temp_file.write(reply->readAll());
}


I don't know any way to download the file directly from QXML, i think you should download your ressource using QNetwork first and parse it after.


QtXML doesn't do any networking itself. It operates on QIODevices, which is a generic interface for anything doing I/O (files, network sockets, ...). You can either download the XML to temp file and then parse it, or, if you parse incrementally e.g. using QXmlStreamReader, parse the data directly as it arrives:

QNetworkAccessManager *manager = new QNetworkAccessManager( this );
QNetworkReply* reply = QNetworkAccessManager::get( manager->get( QNetworkRequest( QUrl("http://www.foo.com/example.xml") ) ) ;
QXmlStreamReader reader( reply );
//...parse

Note that QXmlStreamReader::PrematureEndOfDocumentErrors can occurr while parsing, if there is not enough data yet downloaded. You can either connect to the reply's readyRead() signal to continue, or use reply->waitForReadyRead() if you're parsing outside of the UI thread.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜