Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Toutes les fonctions  ·  Vues d'ensemble  · 

TrafficInfo Example

Files:

Shows how XQuery can be used extract information from WML documents provided by a WAP service.

Overview

The WAP service used in this example is wap.trafikanten.no that is run by the Norwegian governmental agency for public transport in Oslo. The service provides real time information about the departure of busses, trams and undergrounds for every station in the city area.

This example application displays the departure information for a specific station and provides the feature to filter for a special bus or tram line.

Retrieving the Data

Without the knowledge of XQuery, one would use QNetworkAccessManager to query the WML document from the WAP service and then using the QDom classes or QXmlStreamReader classes to iterate over the document and extract the needed information. However this approach results in a lot of glue code and consumes valuable developer time, so we are looking for something that can access XML documents locally or over the network and extract data according to given filter rules. That's the point where XQuery enters the stage!

If we want to know when the underground number 6 in direction Åsjordet is passing the underground station in Nydalen on November 14th 2008 after 1pm, we use the following URL:

http://wap.trafikanten.no/F.asp?f=03012130&t=13&m=00&d=14.11.2008&start=1

The parameters have the following meanings:

  • f The unique station ID of Nydalen.
  • t The hour in 0-23 format.
  • m The minute in 0-59 format.
  • d The date in dd.mm.yyyy format.
  • start Not interesting for our use but should be passed.

As a result we get the following document:

 <?xml version="1.0" encoding="iso-8859-1"?>
 <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
 <wml>
 <template>
     <do type="prev" name="b" label="Tilbake"><prev/></do>
     <do type="options" name="n" label="Nytt sk"><go href="velkommen.wml"/></do>
 </template>
 <card id="Liste" title="Trafikanten">
 <p>
 <small>
 Fra Nydalen [T-bane]:<br />

     <a href="Rute.asp?d=3011030&amp;t=21832&amp;l=4&amp;Start=1">13.00</a>
      4 Bergkrystallen [T-bane]<br />

     <a href="Rute.asp?d=3012585&amp;t=22543&amp;l=6&amp;Start=1">13.05</a>
      6 sjordet<br />

     <a href="Rute.asp?d=3011730&amp;t=22264&amp;l=5&amp;Start=1">13.09</a>
      5 Vestli [T-bane]<br />

     <a href="Rute.asp?d=3012120&amp;t=22080&amp;l=5&amp;Start=1">13.13</a>
      5 Storo [T-bane]<br />

     <a href="Rute.asp?d=3011030&amp;t=21831&amp;l=4&amp;Start=1">13.15</a>
      4 Bergkrystallen [T-bane]<br />

     <a href="Rute.asp?d=3012585&amp;t=22542&amp;l=6&amp;Start=1">13.20</a>
      6 sjordet<br />

     <a href="Rute.asp?d=3011730&amp;t=22263&amp;l=5&amp;Start=1">13.24</a>
      5 Vestli [T-bane]<br />

     <a href="Rute.asp?d=3012120&amp;t=22079&amp;l=5&amp;Start=1">13.28</a>
      5 Storo [T-bane]<br />

     <a href="Rute.asp?d=3011030&amp;t=21830&amp;l=4&amp;Start=1">13.30</a>
      4 Bergkrystallen [T-bane]<br />

     <a href="Rute.asp?d=3012585&amp;t=22541&amp;l=6&amp;Start=1">13.35</a>
      6 sjordet<br />

     <br />
     <a title="Neste 10" href="F.asp?f=03012130&amp;t=13&amp;m=35&amp;d=14.11.2008&amp;Start=11">Neste 10 avganger</a>

 <br/>
 <a href="F.asp?f=03012130&amp;t=14&amp;d=14.11.2008&amp;Start=1">Neste timeintervall</a>
 <br/>
 <a href="F.asp?f=03012130&amp;t=12&amp;d=14.11.2008&amp;Start=1">Forrige timeintervall</a>
 <br/>
 <a href="Velkommen.wml">"Nytt sk"</a>
 <br/>
 </small>
 </p>
 </card>
 </wml>

So for every departure we have a <a> tag that contains the time as a text element, and the following text element contains the line number and direction.

To encapsulate the XQuery code in the example application, we create a custom TimeQuery class. This provides the queryInternal() function that takes a station ID and date/time as input and returns the list of times and directions:

 TimeInformation::List TimeQuery::queryInternal(const QString &stationId, const QDateTime &dateTime)
 {
     const QString timesQueryUrl = QString("doc('http://wap.trafikanten.no/F.asp?f=%1&amp;t=%2&amp;m=%3&amp;d=%4&amp;start=1')/wml/card/p/small/a[fn:starts-with(@href, 'Rute')]/string()")
                                          .arg(stationId)
                                          .arg(dateTime.time().hour())
                                          .arg(dateTime.time().minute())
                                          .arg(dateTime.toString("dd.MM.yyyy"));
     const QString directionsQueryUrl = QString("doc('http://wap.trafikanten.no/F.asp?f=%1&amp;t=%2&amp;m=%3&amp;d=%4&amp;start=1')/wml/card/p/small/text()[matches(., '[0-9].*')]/string()")
                                               .arg(stationId)
                                               .arg(dateTime.time().hour())
                                               .arg(dateTime.time().minute())
                                               .arg(dateTime.toString("dd.MM.yyyy"));

     QStringList times;
     QStringList directions;

     QXmlQuery query;
     query.setQuery(timesQueryUrl);
     query.evaluateTo(&times);

     query.setQuery(directionsQueryUrl);
     query.evaluateTo(&directions);

     if (times.count() != directions.count()) // something went wrong...
         return TimeInformation::List();

     TimeInformation::List information;
     for (int i = 0; i < times.count(); ++i)
         information.append(TimeInformation(times.at(i).simplified(), directions.at(i).simplified()));

     return information;
 }

The first lines of this function synthesize the XQuery strings that fetch the document and extract the data. For better readability, two separated queries are used here: the first one fetches the times and the second fetches the line numbers and directions.

The doc() XQuery method opens a local or remote XML document and returns it, so the /wml/card/p/small/ statement behind it selects all XML nodes that can be reached by the path, wmlcardpsmall. Now we are on the node that contains all the XML nodes we are interested in.

In the first query we select all a nodes that have a href attribute starting with the string "Rute" and return the text of these nodes.

In the second query we select all text nodes that are children of the small node which start with a number. These two queries are passed to the QXmlQuery instance and are evaluated to string lists. After some sanity checking, we have collected all the information we need.

In the section above we have seen that an unique station ID must be passed as an argument to the URL for retrieving the time, so how to find out which is the right station ID to use? The WAP service provides a page for that as well, so the URL

http://wap.trafikanten.no/FromLink1.asp?fra=Nydalen

will return the following document:

 <?xml version="1.0" encoding="iso-8859-1"?>
 <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
 <wml>
 <template>
     <do type="prev" name="b" label="Tilbake"><prev/></do>
     <do type="options" label="Nytt sk"><go href="velkommen.wml"/></do>
 </template>
 <card id="Liste" title="Trafikanten">
 <p>
 <small>
 Velg stoppsted: <br />

    <a title="Velg" href="DateLink.asp?fra=05280320">Nydalen (stre Toten) (-T)</a><br />

    <a title="Velg" href="DateLink.asp?fra=03012126">Nydalen st. (i Store ringvei) (OSL)</a><br />

    <a title="Velg" href="DateLink.asp?fra=03012131">Nydalen T [buss] (OSL)</a><br />

    <a title="Velg" href="DateLink.asp?fra=03012130">Nydalen [T-bane] (OSL)</a><br />

    <a title="Velg" href="DateLink.asp?fra=03012125">Nydalen [tog] (OSL)</a><br />

 <br/>
 <a title="Nytt sk" href="Velkommen.wml">"Nytt sk"</a>
 <br/>
 </small>
 </p>
 </card>
 </wml>

The names of the available stations are listed as separate text elements and the station ID is part of the href attribute of the parent a (anchor) element. In our example, the StationQuery class encapsulates the action of querying the stations that match the given name pattern with the following code:

 StationInformation::List StationQuery::query(const QString &name)
 {
     const QString stationIdQueryUrl = QString("doc(concat('http://wap.trafikanten.no/FromLink1.asp?fra=', $station))/wml/card/p/small/a[@title='Velg']/substring(@href,18)");
     const QString stationNameQueryUrl = QString("doc(concat('http://wap.trafikanten.no/FromLink1.asp?fra=', $station))/wml/card/p/small/a[@title='Velg']/string()");

     QStringList stationIds;
     QStringList stationNames;

     QXmlQuery query;

     query.bindVariable("station", QVariant(QString::fromLatin1(QUrl::toPercentEncoding(name))));
     query.setQuery(stationIdQueryUrl);
     query.evaluateTo(&stationIds);

     query.bindVariable("station", QVariant(QString::fromLatin1(QUrl::toPercentEncoding(name))));
     query.setQuery(stationNameQueryUrl);
     query.evaluateTo(&stationNames);

     if (stationIds.count() != stationNames.count()) // something went wrong...
         return StationInformation::List();

     StationInformation::List information;
     for (int i = 0; i < stationIds.count(); ++i)
         information.append(StationInformation(stationIds.at(i), stationNames.at(i)));

     return information;
 }

Just as in the TimeQuery implementation, the first step is to synthesize the XQuery strings for selecting the station names and the station IDs. As the station name that we pass in the URL will be input from the user, we should protect the XQuery from code injection by using the QXmlQuery::bindVariable() method to do proper quoting of the variable content for us instead of concatenating the two strings manually.

So, we define a XQuery $station variable that is bound to the user input. This variable is concatenated inside the XQuery code with the concat method. To extract the station IDs, we select all a elements that have an title attribute with the content "Velg", and from these elements we take the substring of the href attribute that starts at the 18th character.

The station name can be extracted a bit more easily by just taking the text elements of the selected elements.

After some sanity checks we have all the station IDs and the corresponding names available.

The rest of the code in this example is just for representing the time and station information to the user, and uses techniques described in the Widgets Examples.

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 94
  2. Apercevoir la troisième dimension ou l'utilisation multithreadée d'OpenGL dans Qt, un article des Qt Quarterly traduit par Guillaume Belz 0
  3. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  4. Pourquoi les programmeurs sont-ils moins payés que les gestionnaires de programmes ? Manquent-ils de pouvoir de négociation ? 42
  5. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  6. Adieu qmake, bienvenue qbs : Qt Building Suite, un outil déclaratif et extensible pour la compilation de projets Qt 17
  7. 2017 : un quinquennat pour une nouvelle version du C++ ? Possible, selon Herb Sutter 8
Page suivante

Le Qt Developer Network au hasard

Logo

Installation de PySide : binaires et compilation

Le Qt Developer Network est un réseau de développeurs Qt anglophone, où ils peuvent partager leur expérience sur le framework. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 4.6-snapshot
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web