File: Weblogs.java

package info (click to toggle)
kxml2 2.3.0%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,192 kB
  • ctags: 1,319
  • sloc: java: 5,607; xml: 82; makefile: 18
file content (77 lines) | stat: -rw-r--r-- 2,232 bytes parent folder | download | duplicates (4)
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
import org.xmlpull.v1.*;

import java.util.*;
import java.io.*;
import java.net.*;

/** 
 * A simple example illustrationg some differences of the XmlPull API 
 * and SAX. For the corresponding SAX based implementation, please refer to 
 * http://www.cafeconleche.org/slides/sd2001east/xmlandjava/81.html ff. */

public class Weblogs {

    static List listChannels()
        throws IOException, XmlPullParserException {
        return listChannels("http://static.userland.com/weblogMonitor/logs.xml");
    }

    static List listChannels(String uri)
        throws IOException, XmlPullParserException {

        Vector result = new Vector();

        InputStream is = new URL(uri).openStream();
        XmlPullParser parser =
            XmlPullParserFactory.newInstance().newPullParser();

        parser.setInput(is, null);

        parser.nextTag();
        parser.require(XmlPullParser.START_TAG, "", "weblogs");

        while (parser.nextTag() == XmlPullParser.START_TAG) {
            String url = readSingle(parser);
            if (url != null)
                result.addElement(url);
        }
        parser.require(XmlPullParser.END_TAG, "", "weblogs");

        parser.next();
        parser.require(XmlPullParser.END_DOCUMENT, null, null);

		is.close ();
		parser.setInput (null);

        return result;
    }

    public static String readSingle(XmlPullParser parser)
        throws IOException, XmlPullParserException {

        String url = null;
        parser.require(XmlPullParser.START_TAG, "", "log");

        while (parser.nextTag() == XmlPullParser.START_TAG) {
            String name = parser.getName();
            String content = parser.nextText();
            if (name.equals("url"))
                url = content;
            parser.require(XmlPullParser.END_TAG, "", name);
        }
        parser.require(XmlPullParser.END_TAG, "", "log");
        return url;
    }

    public static void main(String[] args)
        throws IOException, XmlPullParserException {

        List urls =
            args.length > 0
                ? listChannels(args[0])
                : listChannels();

        for (Iterator i = urls.iterator(); i.hasNext();)
            System.out.println(i.next());
    }
}