Example usage for android.sax RootElement getChild

List of usage examples for android.sax RootElement getChild

Introduction

In this page you can find the example usage for android.sax RootElement getChild.

Prototype

public Element getChild(String localName) 

Source Link

Document

Gets the child element with the given name.

Usage

From source file:uk.co.jarofgreen.cityoutdoors.API.BaseCall.java

protected void makeCall(RootElement root) {
    Element errorTag = root.getChild("error");
    errorTag.setStartElementListener(new StartElementListener() {
        public void start(Attributes attributes) {
            errorCode = attributes.getValue("code");
        }/*w  ww .  j a  va 2s. co  m*/
    });
    errorTag.setEndTextElementListener(new EndTextElementListener() {
        public void end(String body) {
            errorMessage = body;
        }
    });

    try {
        httppost.setEntity(multipartEntity);
        response = httpclient.execute(httppost);
        entity = response.getEntity();
        is = entity.getContent();

        Xml.parse(is, Xml.Encoding.UTF_8, root.getContentHandler());
    } catch (SocketException e) {
        errorCode = "A1";
        errorMessage = "Can not connect to internet";
    } catch (UnknownHostException e) {
        errorCode = "A2";
        errorMessage = "Can not connect to internet";
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.codebutler.rsp.Playlist.java

public void fetchItems(final FetchItemsProgressListener progressListener) throws Exception {
    if (mItems != null)
        throw new Exception("fetchItems() already called!");

    mItems = new SortedArrayList<Item>(new Comparator<Item>() {
        public int compare(Item first, Item second) {
            return first.getTitle().compareToIgnoreCase(second.getTitle());
        }/*www. j  a  v  a2 s.c o m*/
    });

    mOrderedArtists = new SortedArrayList<Artist>(new Comparator<Artist>() {
        public int compare(Artist first, Artist second) {
            return first.getName().compareToIgnoreCase(second.getName());
        }
    });

    mArtists = new HashMap<String, Artist>();

    mAlbums = new SortedArrayList<Album>(new Comparator<Album>() {
        public int compare(Album first, Album second) {
            return first.getName().compareToIgnoreCase(second.getName());
        }
    });

    URL url = mServer.buildUrl("db/" + Integer.toString(mId));
    Log.d("FetchItems", url.toString());

    DefaultHttpClient client = new DefaultHttpClient();

    String password = mServer.getPassword();
    if (password != null && password.length() > 0) {
        UsernamePasswordCredentials creds;
        creds = new UsernamePasswordCredentials("user", password);
        client.getCredentialsProvider().setCredentials(AuthScope.ANY, creds);
    }

    HttpGet method = new HttpGet(url.toURI());

    InputStream stream = client.execute(method).getEntity().getContent();

    try {
        /*
         <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
         <response>
        <status>
            <errorcode>0</errorcode>
            <errorstring></errorstring>
            <records>0</records>
            <totalrecords>1</totalrecords>
        </status>
        <items>
            <item>
                <id>1</id>
                <title>Rock Robotic (Osx Mix)</title>
                <artist>Oscillator X</artist>
                <album>Techno * Techyes</album>
                <genre>Dance &amp; DJ</genre>
                <type>mp3</type>
                <bitrate>192</bitrate>
                <samplerate>44100</samplerate>
                <song_length>61440</song_length>
                <file_size>1474560</file_size>
                <year>0</year>
                <track>15</track>
                <total_tracks>0</total_tracks>
                <disc>0</disc>
                <total_discs>0</total_discs>
                <bpm>0</bpm>
                <compilation>0</compilation>
                <rating>0</rating>
                <play_count>4</play_count>
                <description>MPEG audio file</description>
                <time_added>1270096204</time_added>
                <time_modified>1270078936</time_modified>
                <time_played>1270156178</time_played>
                <disabled>0</disabled>
                <codectype>mpeg</codectype>
            </item>
        </items>
         </response>
         */

        RootElement rootElement = new RootElement("response");
        Element itemsElement = rootElement.getChild("items");
        Element itemElement = itemsElement.getChild("item");
        Element idElement = itemElement.getChild("id");
        Element titleElement = itemElement.getChild("title");
        Element artistElement = itemElement.getChild("artist");
        Element albumElement = itemElement.getChild("album");
        Element lengthElement = itemElement.getChild("song_length");

        itemElement.setElementListener(new ElementListener() {
            public void start(Attributes arg0) {
                mCurrentItem = new Item(Playlist.this);
            }

            public void end() {
                mItems.add(mCurrentItem);

                // Artist cache
                String artistName = mCurrentItem.getArtist();
                if (artistName != null && artistName.trim().length() > 0) {
                    Artist artist = mArtists.get(artistName);
                    if (artist == null) {
                        artist = new Artist(artistName);
                        mArtists.put(artistName, artist);
                        mOrderedArtists.add(artist);
                    }

                    // Album cache
                    // FIXME: This should support compilation albums!
                    String albumName = mCurrentItem.getAlbum();
                    if (albumName != null && albumName.trim().length() > 0) {
                        Album album = artist.getAlbum(albumName);
                        if (album == null) {
                            album = new Album(artist, albumName);
                            artist.addAlbum(album);
                            mAlbums.add(album);
                        }
                        album.addItem(mCurrentItem);
                    }
                }

                mCurrentItem = null;

                if (progressListener != null) {
                    if ((mItems.size() % 100) == 0)
                        progressListener.onProgressChange(mItems.size());
                }
            }
        });

        idElement.setEndTextElementListener(new EndTextElementListener() {
            public void end(String body) {
                mCurrentItem.setId(Integer.parseInt(body));
            }
        });

        titleElement.setEndTextElementListener(new EndTextElementListener() {
            public void end(String body) {
                mCurrentItem.setTitle(body);
            }
        });

        artistElement.setEndTextElementListener(new EndTextElementListener() {
            public void end(String body) {
                mCurrentItem.setArtist(body);
            }
        });

        albumElement.setEndTextElementListener(new EndTextElementListener() {
            public void end(String body) {
                mCurrentItem.setAlbum(body);
            }
        });

        lengthElement.setEndTextElementListener(new EndTextElementListener() {
            public void end(String body) {
                mCurrentItem.setDuration(Long.parseLong(body) / 1000);
            }
        });

        android.util.Xml.parse(stream, Xml.Encoding.UTF_8, rootElement.getContentHandler());
    } finally {
        stream.close();
    }
}