summaryrefslogtreecommitdiffstats
path: root/docs/html/training/basics/network-ops/xml.jd
blob: b148257ec7c353c08e235d94789a06da5894f31c (plain)
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
page.title=Parsing XML Data
parent.title=Performing Network Operations
parent.link=index.html

trainingnavtop=true

previous.title=Managing Network Usage
previous.link=managing.html

@jd:body

<div id="tb-wrapper"> 
<div id="tb">



<h2>This lesson teaches you to</h2>
<ol>
  <li><a href="#choose">Choose a Parser</a></li>
  <li><a href="#analyze">Analyze the Feed</a></li>
  <li><a href="#instantiate">Instantiate the Parser</a></li>
  <li><a href="#read">Read the Feed</a></li>
  <li><a href="#parse">Parse XML</a></li>
  <li><a href="#skip">Skip Tags You Don't Care About</a></li>
  <li><a href="#consume">Consume XML Data</a></li>
</ol>

<h2>You should also read</h2>
<ul>
  <li><a href="{@docRoot}guide/webapps/index.html">Web Apps Overview</a></li>
</ul>

<h2>Try it out</h2>

<div class="download-box">
  <a href="{@docRoot}shareables/training/NetworkUsage.zip"
class="button">Download the sample</a>
 <p class="filename">NetworkUsage.zip</p>
</div>

</div> 
</div>

<p>Extensible Markup Language (XML) is a set of rules for encoding documents in
machine-readable form. XML is a popular format for sharing data on the internet.
Websites that frequently update their content, such as news sites or blogs,
often provide an XML feed so that external programs can keep abreast of content
changes. Uploading and parsing XML data is a common task for network-connected
apps. This lesson explains how to parse XML documents and use their data.</p>

<h2 id="choose">Choose a Parser</h2>

<p>We recommend {@link org.xmlpull.v1.XmlPullParser}, which is an efficient and
maintainable way to parse XML on Android. Historically Android has had two
implementations of this interface:</p>

<ul>
  <li><a href="http://kxml.sourceforge.net/"><code>KXmlParser</code></a> 
  via {@link org.xmlpull.v1.XmlPullParserFactory#newPullParser XmlPullParserFactory.newPullParser()}. 
  </li>
  <li><code>ExpatPullParser</code>, via 
  {@link android.util.Xml#newPullParser Xml.newPullParser()}. 
  </li>
</ul>

<p>Either choice is fine. The
example in this section uses <code>ExpatPullParser</code>, via
{@link android.util.Xml#newPullParser Xml.newPullParser()}. </p>

<h2 id="analyze">Analyze the Feed</h2>

<p>The first step in parsing a feed is to decide which fields you're interested in. 
The parser extracts data for those fields and ignores the rest.</p>

<p>Here is an excerpt from the feed that's being parsed in the sample app. Each
post to <a href="http://stackoverflow.com">StackOverflow.com</a> appears in the
feed as an <code>entry</code> tag that contains several nested tags:</p>

<pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt; 
&lt;feed xmlns=&quot;http://www.w3.org/2005/Atom&quot; xmlns:creativeCommons=&quot;http://backend.userland.com/creativeCommonsRssModule&quot; ...&quot;&gt;     
&lt;title type=&quot;text&quot;&gt;newest questions tagged android - Stack Overflow&lt;/title&gt;
...
    &lt;entry&gt;
    ...
    &lt;/entry&gt;
    &lt;entry&gt;
        &lt;id&gt;http://stackoverflow.com/q/9439999&lt;/id&gt;
        &lt;re:rank scheme="http://stackoverflow.com"&gt;0&lt;/re:rank&gt;
        &lt;title type="text"&gt;Where is my data file?&lt;/title&gt;
        &lt;category scheme="http://stackoverflow.com/feeds/tag?tagnames=android&amp;sort=newest/tags" term="android"/&gt;
        &lt;category scheme="http://stackoverflow.com/feeds/tag?tagnames=android&amp;sort=newest/tags" term="file"/&gt;
        &lt;author&gt;
            &lt;name&gt;cliff2310&lt;/name&gt;
            &lt;uri&gt;http://stackoverflow.com/users/1128925&lt;/uri&gt;
        &lt;/author&gt;
        &lt;link rel="alternate" href="http://stackoverflow.com/questions/9439999/where-is-my-data-file" /&gt;
        &lt;published&gt;2012-02-25T00:30:54Z&lt;/published&gt;
        &lt;updated&gt;2012-02-25T00:30:54Z&lt;/updated&gt;
        &lt;summary type="html"&gt;
            &lt;p&gt;I have an Application that requires a data file...&lt;/p&gt;

        &lt;/summary&gt;
    &lt;/entry&gt;
    &lt;entry&gt;
    ...
    &lt;/entry&gt;
...
&lt;/feed&gt;</pre>

<p>The sample app  
extracts data for the <code>entry</code> tag and its nested tags
<code>title</code>, <code>link</code>, and <code>summary</code>.</p>


<h2 id="instantiate">Instantiate the Parser</h2>

<p>The next step is to
instantiate a parser and kick off the parsing process. In this snippet, a parser
is initialized to not process namespaces, and to use the provided {@link
java.io.InputStream} as its input. It starts the parsing process with a call to
{@link org.xmlpull.v1.XmlPullParser#nextTag() nextTag()} and invokes the
<code>readFeed()</code> method, which extracts and processes the data the app is
interested in:</p>

<pre>public class StackOverflowXmlParser {
    // We don't use namespaces
    private static final String ns = null;
   
    public List<Entry> parse(InputStream in) throws XmlPullParserException, IOException {
        try {
            XmlPullParser parser = Xml.newPullParser();
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
            parser.setInput(in, null);
            parser.nextTag();
            return readFeed(parser);
        } finally {
            in.close();
        }
    }
 ... 
}</pre>

<h2 id="read">Read the Feed</h2>

<p>The <code>readFeed()</code> method does the actual work of processing the
feed. It looks for elements tagged "entry" as a starting point for recursively
processing the feed. If a tag isn't an {@code entry} tag, it skips it. Once the whole
feed has been recursively processed, <code>readFeed()</code> returns a {@link
java.util.List} containing the entries (including nested data members) it
extracted from the feed. This {@link java.util.List} is then returned by the
parser.</p>

<pre>
private List<Entry> readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
    List<Entry> entries = new ArrayList<Entry>();

    parser.require(XmlPullParser.START_TAG, ns, "feed");
    while (parser.next() != XmlPullParser.END_TAG) {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            continue;
        }
        String name = parser.getName();
        // Starts by looking for the entry tag
        if (name.equals("entry")) {
            entries.add(readEntry(parser));
        } else {
            skip(parser);
        }
    }  
    return entries;
}</pre>


<h2 id="parse">Parse XML</h2>


<p>The steps for parsing an XML feed are as follows:</p>
<ol>

  <li>As described in <a href="#analyze">Analyze the Feed</a>, identify the tags you want to include in your app. This
example extracts data for the <code>entry</code> tag and its nested tags
<code>title</code>, <code>link</code>, and <code>summary</code>.</li>

<li>Create the following methods:</p>

<ul>

<li>A "read" method for each tag you're interested in. For example,
<code>readEntry()</code>, <code>readTitle()</code>, and so on. The parser reads
tags from the input stream. When it encounters a tag named <code>entry</code>, 
<code>title</code>,
<code>link</code> or <code>summary</code>, it calls the appropriate method
for that tag. Otherwise, it skips the tag.
</li>

<li>Methods to extract data for each different type of tag and to advance the
parser to the next tag. For example:
<ul>

<li>For the <code>title</code> and <code>summary</code> tags, the parser calls
<code>readText()</code>. This method extracts data for these tags by calling
<code>parser.getText()</code>.</li>

<li>For the <code>link</code> tag, the parser extracts data for links by first 
determining if the link is the kind
it's interested in. Then it uses <code>parser.getAttributeValue()</code> to
extract the link's value.</li>

<li>For the <code>entry</code> tag, the parser calls <code>readEntry()</code>.
This method parses the entry's nested tags and returns an <code>Entry</code>
object with the data members <code>title</code>, <code>link</code>, and
<code>summary</code>.</li>

</ul>
</li>
<li>A helper <code>skip()</code> method that's recursive. For more discussion of this topic, see <a href="#skip">Skip Tags You Don't Care About</a>.</li>
</ul>
  
  </li>
</ol>

<p>This snippet shows how the parser parses entries, titles, links, and summaries.</p>
<pre>public static class Entry {
    public final String title;
    public final String link;
    public final String summary;

    private Entry(String title, String summary, String link) {
        this.title = title;
        this.summary = summary;
        this.link = link;
    }
}
  
// Parses the contents of an entry. If it encounters a title, summary, or link tag, hands them off
// to their respective &quot;read&quot; methods for processing. Otherwise, skips the tag.
private Entry readEntry(XmlPullParser parser) throws XmlPullParserException, IOException {
    parser.require(XmlPullParser.START_TAG, ns, "entry");
    String title = null;
    String summary = null;
    String link = null;
    while (parser.next() != XmlPullParser.END_TAG) {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            continue;
        }
        String name = parser.getName();
        if (name.equals("title")) {
            title = readTitle(parser);
        } else if (name.equals("summary")) {
            summary = readSummary(parser);
        } else if (name.equals("link")) {
            link = readLink(parser);
        } else {
            skip(parser);
        }
    }
    return new Entry(title, summary, link);
}

// Processes title tags in the feed.
private String readTitle(XmlPullParser parser) throws IOException, XmlPullParserException {
    parser.require(XmlPullParser.START_TAG, ns, "title");
    String title = readText(parser);
    parser.require(XmlPullParser.END_TAG, ns, "title");
    return title;
}
  
// Processes link tags in the feed.
private String readLink(XmlPullParser parser) throws IOException, XmlPullParserException {
    String link = "";
    parser.require(XmlPullParser.START_TAG, ns, "link");
    String tag = parser.getName();
    String relType = parser.getAttributeValue(null, "rel");  
    if (tag.equals("link")) {
        if (relType.equals("alternate")){
            link = parser.getAttributeValue(null, "href");
            parser.nextTag();
        } 
    }
    parser.require(XmlPullParser.END_TAG, ns, "link");
    return link;
}

// Processes summary tags in the feed.
private String readSummary(XmlPullParser parser) throws IOException, XmlPullParserException {
    parser.require(XmlPullParser.START_TAG, ns, "summary");
    String summary = readText(parser);
    parser.require(XmlPullParser.END_TAG, ns, "summary");
    return summary;
}

// For the tags title and summary, extracts their text values.
private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
    String result = "";
    if (parser.next() == XmlPullParser.TEXT) {
        result = parser.getText();
        parser.nextTag();
    }
    return result;
}
  ...
}</pre>

<h2 id="skip">Skip Tags You Don't Care About</h2>

<p>One of the steps in the XML parsing described above is for the parser to skip tags it's not interested in. Here is the parser's <code>skip()</code> method:</p>

<pre>
private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
    if (parser.getEventType() != XmlPullParser.START_TAG) {
        throw new IllegalStateException();
    }
    int depth = 1;
    while (depth != 0) {
        switch (parser.next()) {
        case XmlPullParser.END_TAG:
            depth--;
            break;
        case XmlPullParser.START_TAG:
            depth++;
            break;
        }
    }
 }
</pre>

<p>This is how it works:</p>

<ul>

<li>It throws an exception if the current event isn't a
<code>START_TAG</code>.</li>

<li>It consumes the <code>START_TAG</code>, and all events up to and including
the matching <code>END_TAG</code>.</li>

<li>To make sure that it stops at the correct <code>END_TAG</code> and not at
the first tag it encounters after the original <code>START_TAG</code>, it keeps
track of the nesting depth.</li>

</ul>

<p>Thus if the current element has nested elements, the value of
<code>depth</code> won't be 0 until the parser has consumed all events between
the original <code>START_TAG</code> and its matching <code>END_TAG</code>. For
example, consider how the parser skips the <code>&lt;author&gt;</code> element,
which has 2 nested elements, <code>&lt;name&gt;</code> and
<code>&lt;uri&gt;</code>:</p>

<ul>

<li>The first time through the <code>while</code> loop, the next tag the parser
encounters after <code>&lt;author&gt;</code> is the <code>START_TAG</code> for 
<code>&lt;name&gt;</code>. The value for <code>depth</code> is incremented to
2.</li>

<li>The second time through the <code>while</code> loop, the next tag the parser
encounters is the <code>END_TAG</code>  <code>&lt;/name&gt;</code>. The value
for <code>depth</code> is decremented to 1.</li>

<li>The third time through the <code>while</code> loop, the next tag the parser
encounters is the <code>START_TAG</code>  <code>&lt;uri&gt;</code>. The value
for <code>depth</code> is incremented to 2.</li>

<li>The fourth time through the <code>while</code> loop, the next tag the parser
encounters is the <code>END_TAG</code>  <code>&lt;/uri&gt;</code>. The value for
<code>depth</code> is decremented to 1.</li>

<li>The fifth time and final time through the <code>while</code> loop, the next
tag the parser encounters is the <code>END_TAG</code> 
<code>&lt;/author&gt;</code>. The value for <code>depth</code> is decremented to
0, indicating that the <code>&lt;author&gt;</code> element has been successfully
skipped.</li>

</ul>

<h2 id="consume">Consume XML Data</h2>

<p>The example application fetches and parses the XML feed within an {@link
android.os.AsyncTask}. This takes the processing off the main UI thread. When 
processing is complete, the app updates the UI in the main activity
(<code>NetworkActivity</code>).</p>
<p>In the excerpt shown below, the <code>loadPage()</code> method does the
following:</p>

<ul>

  <li>Initializes a string variable with the URL for the XML feed.</li>
  
  <li>If the user's settings and the network connection allow it, invokes
<code>new DownloadXmlTask().execute(url)</code>. This instantiates a new 
<code>DownloadXmlTask</code> object ({@link android.os.AsyncTask} subclass) and
runs its {@link android.os.AsyncTask#execute execute()} method, which downloads
and parses the feed and returns a string result to be displayed in the UI.</li>
  
</ul>
<pre>
public class NetworkActivity extends Activity {
    public static final String WIFI = "Wi-Fi";
    public static final String ANY = "Any";
    private static final String URL = "http://stackoverflow.com/feeds/tag?tagnames=android&sort=newest";
   
    // Whether there is a Wi-Fi connection.
    private static boolean wifiConnected = false; 
    // Whether there is a mobile connection.
    private static boolean mobileConnected = false;
    // Whether the display should be refreshed.
    public static boolean refreshDisplay = true; 
    public static String sPref = null;

    ...
      
    // Uses AsyncTask to download the XML feed from stackoverflow.com.
    public void loadPage() {  
      
        if((sPref.equals(ANY)) && (wifiConnected || mobileConnected)) {
            new DownloadXmlTask().execute(URL);
        }
        else if ((sPref.equals(WIFI)) && (wifiConnected)) {
            new DownloadXmlTask().execute(URL);
        } else {
            // show error
        }  
    }</pre>
    
<p>The {@link android.os.AsyncTask} subclass shown below,
<code>DownloadXmlTask</code>, implements the following {@link
android.os.AsyncTask} methods:</p>

    <ul>
    
      <li>{@link android.os.AsyncTask#doInBackground doInBackground()} executes
the method <code>loadXmlFromNetwork()</code>. It passes the feed URL as a
parameter. The method <code>loadXmlFromNetwork()</code> fetches and processes
the feed. When it finishes, it passes back a result string.</li>
      
      <li>{@link android.os.AsyncTask#onPostExecute onPostExecute()} takes the
returned string and displays it in the UI.</li>
      
    </ul>
    
<pre>
// Implementation of AsyncTask used to download XML feed from stackoverflow.com.
private class DownloadXmlTask extends AsyncTask&lt;String, Void, String&gt; {
    &#64;Override
    protected String doInBackground(String... urls) {
        try {
            return loadXmlFromNetwork(urls[0]);
        } catch (IOException e) {
            return getResources().getString(R.string.connection_error);
        } catch (XmlPullParserException e) {
            return getResources().getString(R.string.xml_error);
        }
    }

    &#64;Override
    protected void onPostExecute(String result) {  
        setContentView(R.layout.main);
        // Displays the HTML string in the UI via a WebView
        WebView myWebView = (WebView) findViewById(R.id.webview);
        myWebView.loadData(result, "text/html", null);
    }
}</pre>

   <p>Below is the method <code>loadXmlFromNetwork()</code> that is invoked from
<code>DownloadXmlTask</code>. It does the following:</p>
   
   <ol>
   
     <li>Instantiates a <code>StackOverflowXmlParser</code>. It also creates variables for
a {@link java.util.List} of <code>Entry</code> objects (<code>entries</code>), and 
<code>title</code>, <code>url</code>, and <code>summary</code>, to hold the
values extracted from the XML feed for those fields.</li>
     
     <li>Calls <code>downloadUrl()</code>, which fetches the feed and returns it as 
     an {@link java.io.InputStream}.</li>
     
     <li>Uses <code>StackOverflowXmlParser</code> to parse the {@link java.io.InputStream}. 
     <code>StackOverflowXmlParser</code> populates a 
     {@link java.util.List} of <code>entries</code> with data from the feed.</li>
     
     <li>Processes the <code>entries</code> {@link java.util.List}, 
 and combines the feed data with HTML markup.</li>
     
     <li>Returns an HTML string that is displayed in the main activity
UI by the {@link android.os.AsyncTask} method {@link
android.os.AsyncTask#onPostExecute onPostExecute()}.</li>
     
</ol>

<pre>
// Uploads XML from stackoverflow.com, parses it, and combines it with
// HTML markup. Returns HTML string.
private String loadXmlFromNetwork(String urlString) throws XmlPullParserException, IOException {
    InputStream stream = null;
    // Instantiate the parser
    StackOverflowXmlParser stackOverflowXmlParser = new StackOverflowXmlParser();
    List&lt;Entry&gt; entries = null;
    String title = null;
    String url = null;
    String summary = null;
    Calendar rightNow = Calendar.getInstance(); 
    DateFormat formatter = new SimpleDateFormat("MMM dd h:mmaa");
        
    // Checks whether the user set the preference to include summary text
    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
    boolean pref = sharedPrefs.getBoolean("summaryPref", false);
        
    StringBuilder htmlString = new StringBuilder();
    htmlString.append("&lt;h3&gt;" + getResources().getString(R.string.page_title) + "&lt;/h3&gt;");
    htmlString.append("&lt;em&gt;" + getResources().getString(R.string.updated) + " " + 
            formatter.format(rightNow.getTime()) + "&lt;/em&gt;");
        
    try {
        stream = downloadUrl(urlString);        
        entries = stackOverflowXmlParser.parse(stream);
    // Makes sure that the InputStream is closed after the app is
    // finished using it.
    } finally {
        if (stream != null) {
            stream.close();
        } 
     }
    
    // StackOverflowXmlParser returns a List (called "entries") of Entry objects.
    // Each Entry object represents a single post in the XML feed.
    // This section processes the entries list to combine each entry with HTML markup.
    // Each entry is displayed in the UI as a link that optionally includes
    // a text summary.
    for (Entry entry : entries) {       
        htmlString.append("&lt;p&gt;&lt;a href='");
        htmlString.append(entry.link);
        htmlString.append("'&gt;" + entry.title + "&lt;/a&gt;&lt;/p&gt;");
        // If the user set the preference to include summary text,
        // adds it to the display.
        if (pref) {
            htmlString.append(entry.summary);
        }
    }
    return htmlString.toString();
}

// Given a string representation of a URL, sets up a connection and gets
// an input stream.
private InputStream downloadUrl(String urlString) throws IOException {
    URL url = new URL(urlString);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setReadTimeout(10000 /* milliseconds */);
    conn.setConnectTimeout(15000 /* milliseconds */);
    conn.setRequestMethod("GET");
    conn.setDoInput(true);
    // Starts the query
    conn.connect();
    InputStream stream = conn.getInputStream();      
}</pre>