1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
|
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- trafficinfo.qdoc -->
<title>Qt 4.8: TrafficInfo Example</title>
<link rel="stylesheet" type="text/css" href="style/offline.css" />
</head>
<body>
<div class="header" id="qtdocheader">
<div class="content">
<a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a>
</div>
<div class="breadcrumb toolblock">
<ul>
<li class="first"><a href="index.html">Home</a></li>
<!-- Breadcrumbs go here -->
<li><a href="all-examples.html">Examples</a></li>
<li>TrafficInfo Example</li>
</ul>
</div>
</div>
<div class="content mainContent">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#overview">Overview</a></li>
<li class="level1"><a href="#retrieving-the-data">Retrieving the Data</a></li>
</ul>
</div>
<h1 class="title">TrafficInfo Example</h1>
<span class="subtitle"></span>
<!-- $$$xmlpatterns/trafficinfo-description -->
<div class="descr"> <a name="details"></a>
<p>Files:</p>
<ul>
<li><a href="xmlpatterns-trafficinfo-mainwindow-cpp.html">xmlpatterns/trafficinfo/mainwindow.cpp</a></li>
<li><a href="xmlpatterns-trafficinfo-mainwindow-h.html">xmlpatterns/trafficinfo/mainwindow.h</a></li>
<li><a href="xmlpatterns-trafficinfo-stationdialog-cpp.html">xmlpatterns/trafficinfo/stationdialog.cpp</a></li>
<li><a href="xmlpatterns-trafficinfo-stationdialog-h.html">xmlpatterns/trafficinfo/stationdialog.h</a></li>
<li><a href="xmlpatterns-trafficinfo-stationdialog-ui.html">xmlpatterns/trafficinfo/stationdialog.ui</a></li>
<li><a href="xmlpatterns-trafficinfo-stationquery-cpp.html">xmlpatterns/trafficinfo/stationquery.cpp</a></li>
<li><a href="xmlpatterns-trafficinfo-stationquery-h.html">xmlpatterns/trafficinfo/stationquery.h</a></li>
<li><a href="xmlpatterns-trafficinfo-timequery-cpp.html">xmlpatterns/trafficinfo/timequery.cpp</a></li>
<li><a href="xmlpatterns-trafficinfo-timequery-h.html">xmlpatterns/trafficinfo/timequery.h</a></li>
<li><a href="xmlpatterns-trafficinfo-main-cpp.html">xmlpatterns/trafficinfo/main.cpp</a></li>
<li><a href="xmlpatterns-trafficinfo-trafficinfo-pro.html">xmlpatterns/trafficinfo/trafficinfo.pro</a></li>
</ul>
<p>The Traffic Info example shows how <a href="xmlprocessing.html">XQuery</a> can be used extract information from WML documents provided by a WAP service.<a name="overview"></a>
<h2>Overview</h2>
<p>The WAP service used in this example is <a href="http://wap.trafikanten.no">wap.trafikanten.no</a> 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.</p>
<p>This example application displays the departure information for a specific station and provides the feature to filter for a special bus or tram line.</p>
<p class="centerAlign"><img src="images/trafficinfo-example.png" alt="" /></p><a name="retrieving-the-data"></a>
<h2>Retrieving the Data</h2>
<p>Without the knowledge of <a href="xmlprocessing.html">XQuery</a>, one would use <a href="qnetworkaccessmanager.html">QNetworkAccessManager</a> to query the WML document from the WAP service and then using the QDom classes or <a href="qxmlstreamreader.html">QXmlStreamReader</a> 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 <a href="xmlprocessing.html">XQuery</a> enters the stage!</p>
<p>If we want to know when the underground number 6 in direction Å<tt></tt>sjordet is passing the underground station in Nydalen on November 14th 2008 after 1pm, we use the following URL:</p>
<p><tt>http://wap.trafikanten.no/F.asp?f=03012130&t=13&m=00&d=14.11.2008&start=1</tt></p>
<p>The parameters have the following meanings:</p>
<ul>
<li><i>f</i> The unique station ID of Nydalen.</li>
<li><i>t</i> The hour in 0-23 format.</li>
<li><i>m</i> The minute in 0-59 format.</li>
<li><i>d</i> The date in dd.mm.yyyy format.</li>
<li><i>start</i> Not interesting for our use but should be passed.</li>
</ul>
<p>As a result we get the following document:</p>
<pre class="cpp"> <?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></pre>
<p>So for every departure we have a <tt><a></tt> tag that contains the time as a text element, and the following text element contains the line number and direction.</p>
<p>To encapsulate the <a href="xmlprocessing.html">XQuery</a> code in the example application, we create a custom <tt>TimeQuery</tt> class. This provides the <tt>queryInternal()</tt> function that takes a station ID and date/time as input and returns the list of times and directions:</p>
<pre class="cpp"> TimeInformation<span class="operator">::</span>List TimeQuery<span class="operator">::</span>queryInternal(<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&</span>stationId<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="qdatetime.html">QDateTime</a></span> <span class="operator">&</span>dateTime)
{
<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> timesQueryUrl <span class="operator">=</span> <span class="type"><a href="qstring.html">QString</a></span>(<span class="string">"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()"</span>)
<span class="operator">.</span>arg(stationId)
<span class="operator">.</span>arg(dateTime<span class="operator">.</span>time()<span class="operator">.</span>hour())
<span class="operator">.</span>arg(dateTime<span class="operator">.</span>time()<span class="operator">.</span>minute())
<span class="operator">.</span>arg(dateTime<span class="operator">.</span>toString(<span class="string">"dd.MM.yyyy"</span>));
<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> directionsQueryUrl <span class="operator">=</span> <span class="type"><a href="qstring.html">QString</a></span>(<span class="string">"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()"</span>)
<span class="operator">.</span>arg(stationId)
<span class="operator">.</span>arg(dateTime<span class="operator">.</span>time()<span class="operator">.</span>hour())
<span class="operator">.</span>arg(dateTime<span class="operator">.</span>time()<span class="operator">.</span>minute())
<span class="operator">.</span>arg(dateTime<span class="operator">.</span>toString(<span class="string">"dd.MM.yyyy"</span>));
<span class="type"><a href="qstringlist.html">QStringList</a></span> times;
<span class="type"><a href="qstringlist.html">QStringList</a></span> directions;
<span class="type"><a href="qxmlquery.html">QXmlQuery</a></span> query;
query<span class="operator">.</span>setQuery(timesQueryUrl);
query<span class="operator">.</span>evaluateTo(<span class="operator">&</span>times);
query<span class="operator">.</span>setQuery(directionsQueryUrl);
query<span class="operator">.</span>evaluateTo(<span class="operator">&</span>directions);
<span class="keyword">if</span> (times<span class="operator">.</span>count() <span class="operator">!</span><span class="operator">=</span> directions<span class="operator">.</span>count()) <span class="comment">// something went wrong...</span>
<span class="keyword">return</span> TimeInformation<span class="operator">::</span>List();
TimeInformation<span class="operator">::</span>List information;
<span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">0</span>; i <span class="operator"><</span> times<span class="operator">.</span>count(); <span class="operator">+</span><span class="operator">+</span>i)
information<span class="operator">.</span>append(TimeInformation(times<span class="operator">.</span>at(i)<span class="operator">.</span>simplified()<span class="operator">,</span> directions<span class="operator">.</span>at(i)<span class="operator">.</span>simplified()));
<span class="keyword">return</span> information;
}</pre>
<p>The first lines of this function synthesize the <a href="xmlprocessing.html">XQuery</a> 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.</p>
<p>The <tt>doc()</tt> <a href="xmlprocessing.html">XQuery</a> method opens a local or remote XML document and returns it, so the <tt>/wml/card/p/small/</tt> statement behind it selects all XML nodes that can be reached by the path, <tt>wml</tt> → <tt>card</tt> → <tt>p</tt> → <tt>small</tt>. Now we are on the node that contains all the XML nodes we are interested in.</p>
<p>In the first query we select all <tt>a</tt> nodes that have a <tt>href</tt> attribute starting with the string "Rute" and return the text of these nodes.</p>
<p>In the second query we select all text nodes that are children of the <tt>small</tt> node which start with a number. These two queries are passed to the <a href="qxmlquery.html">QXmlQuery</a> instance and are evaluated to string lists. After some sanity checking, we have collected all the information we need.</p>
<p>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</p>
<p><tt>http://wap.trafikanten.no/FromLink1.asp?fra=Nydalen</tt></p>
<p>will return the following document:</p>
<pre class="cpp"> <?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></pre>
<p>The names of the available stations are listed as separate text elements and the station ID is part of the <tt>href</tt> attribute of the parent <tt>a</tt> (anchor) element. In our example, the <tt>StationQuery</tt> class encapsulates the action of querying the stations that match the given name pattern with the following code:</p>
<pre class="cpp"> StationInformation<span class="operator">::</span>List StationQuery<span class="operator">::</span>query(<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> <span class="operator">&</span>name)
{
<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> stationIdQueryUrl <span class="operator">=</span> <span class="type"><a href="qstring.html">QString</a></span>(<span class="string">"doc(concat('http://wap.trafikanten.no/FromLink1.asp?fra=', $station))/wml/card/p/small/a[@title='Velg']/substring(@href,18)"</span>);
<span class="keyword">const</span> <span class="type"><a href="qstring.html">QString</a></span> stationNameQueryUrl <span class="operator">=</span> <span class="type"><a href="qstring.html">QString</a></span>(<span class="string">"doc(concat('http://wap.trafikanten.no/FromLink1.asp?fra=', $station))/wml/card/p/small/a[@title='Velg']/string()"</span>);
<span class="type"><a href="qstringlist.html">QStringList</a></span> stationIds;
<span class="type"><a href="qstringlist.html">QStringList</a></span> stationNames;
<span class="type"><a href="qxmlquery.html">QXmlQuery</a></span> query;
query<span class="operator">.</span>bindVariable(<span class="string">"station"</span><span class="operator">,</span> <span class="type"><a href="qvariant.html">QVariant</a></span>(<span class="type"><a href="qstring.html">QString</a></span><span class="operator">::</span>fromLatin1(<span class="type"><a href="qurl.html">QUrl</a></span><span class="operator">::</span>toPercentEncoding(name))));
query<span class="operator">.</span>setQuery(stationIdQueryUrl);
query<span class="operator">.</span>evaluateTo(<span class="operator">&</span>stationIds);
query<span class="operator">.</span>bindVariable(<span class="string">"station"</span><span class="operator">,</span> <span class="type"><a href="qvariant.html">QVariant</a></span>(<span class="type"><a href="qstring.html">QString</a></span><span class="operator">::</span>fromLatin1(<span class="type"><a href="qurl.html">QUrl</a></span><span class="operator">::</span>toPercentEncoding(name))));
query<span class="operator">.</span>setQuery(stationNameQueryUrl);
query<span class="operator">.</span>evaluateTo(<span class="operator">&</span>stationNames);
<span class="keyword">if</span> (stationIds<span class="operator">.</span>count() <span class="operator">!</span><span class="operator">=</span> stationNames<span class="operator">.</span>count()) <span class="comment">// something went wrong...</span>
<span class="keyword">return</span> StationInformation<span class="operator">::</span>List();
StationInformation<span class="operator">::</span>List information;
<span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">0</span>; i <span class="operator"><</span> stationIds<span class="operator">.</span>count(); <span class="operator">+</span><span class="operator">+</span>i)
information<span class="operator">.</span>append(StationInformation(stationIds<span class="operator">.</span>at(i)<span class="operator">,</span> stationNames<span class="operator">.</span>at(i)));
<span class="keyword">return</span> information;
}</pre>
<p>Just as in the <tt>TimeQuery</tt> implementation, the first step is to synthesize the <a href="xmlprocessing.html">XQuery</a> 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 <a href="xmlprocessing.html">XQuery</a> from code injection by using the <a href="qxmlquery.html#bindVariable">QXmlQuery::bindVariable</a>() method to do proper quoting of the variable content for us instead of concatenating the two strings manually.</p>
<p>So, we define a <a href="xmlprocessing.html">XQuery</a> <tt>$station</tt> variable that is bound to the user input. This variable is concatenated inside the <a href="xmlprocessing.html">XQuery</a> code with the <tt>concat</tt> method. To extract the station IDs, we select all <tt>a</tt> elements that have an <tt>title</tt> attribute with the content "Velg", and from these elements we take the substring of the <tt>href</tt> attribute that starts at the 18th character.</p>
<p>The station name can be extracted a bit more easily by just taking the text elements of the selected <i>elements</i>.</p>
<p>After some sanity checks we have all the station IDs and the corresponding names available.</p>
<p>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 <a href="examples-widgets.html">Widget Examples</a>.</p>
</div>
<!-- @@@xmlpatterns/trafficinfo -->
<div class="ft">
<span></span>
</div>
</div>
<div class="footer">
<p>
<acronym title="Copyright">©</acronym> 2012 Nokia Corporation and/or its
subsidiaries. Documentation contributions included herein are the copyrights of
their respective owners.</p>
<br />
<p>
The documentation provided herein is licensed under the terms of the
<a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation
License version 1.3</a> as published by the Free Software Foundation.</p>
<p>
Documentation sources may be obtained from <a href="http://www.qt-project.org">
www.qt-project.org</a>.</p>
<br />
<p>
Nokia, Qt and their respective logos are trademarks of Nokia Corporation
in Finland and/or other countries worldwide. All other trademarks are property
of their respective owners. <a title="Privacy Policy"
href="http://en.gitorious.org/privacy_policy/">Privacy Policy</a></p>
</div>
</body>
</html>
|