Example usage for android.sax Element getChild

List of usage examples for android.sax Element getChild

Introduction

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

Prototype

public Element getChild(String localName) 

Source Link

Document

Gets the child element with the given name.

Usage

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());
        }//  w ww  . j a va 2  s  .co 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();
    }
}

From source file:org.mythdroid.util.UpdateService.java

@Override
public void onCreate() {

    registerReceiver(receiver, filter);/*  w  w w. java 2  s  . com*/

    handler = new XMLHandler("feed"); //$NON-NLS-1$
    final Element root = handler.rootElement();
    final Element entry = root.getChild("entry"); //$NON-NLS-1$

    entry.getChild("content").setTextElementListener( //$NON-NLS-1$
            new EndTextElementListener() {
                @Override
                public void end(String text) {
                    int start = text.indexOf("http:"); //$NON-NLS-1$
                    int end = text.indexOf("\">", start); //$NON-NLS-1$
                    download.url = text.substring(start, end);
                    entries.add(download);
                }
            });

    entry.getChild("title").setTextElementListener( //$NON-NLS-1$
            new EndTextElementListener() {
                @Override
                public void end(String text) {
                    download = new DownloadEntry();
                    download.title = text;

                }
            });

    Globals.runOnWorker(new Runnable() {
        @Override
        public void run() {
            getAvailableVersions();
        }
    });

}

From source file:org.mythdroid.activities.Guide.java

/**
 * Fetch and parse guide data from the backend
 * @param start Date that guide should start at
 * @param end Date that guide should end at
 *///from w  w  w  .j  a  v  a  2  s .  co  m
private void getGuideData(Date start, Date end) {

    if (Globals.haveServices()) {
        try {
            channels = guideService.GetProgramGuide(start, end);
        } catch (IOException e) {
            ErrUtil.postErr(this, e);
            return;
        }
        return;
    }

    // No services api - use MythXML

    XMLHandler handler = new XMLHandler("GetProgramGuideResponse"); //$NON-NLS-1$
    Element root = handler.rootElement();

    root.getChild("NumOfChannels").setTextElementListener( //$NON-NLS-1$
            new EndTextElementListener() {
                @Override
                public void end(String body) {
                    channels.ensureCapacity(Integer.valueOf(body));
                }
            });

    Element chanElement = root.getChild("ProgramGuide") //$NON-NLS-1$
            .getChild("Channels") //$NON-NLS-1$
            .getChild("Channel"); //$NON-NLS-1$

    chanElement.setStartElementListener(new ChannelXMLParser(this, chanElement, new ChannelListener() {
        @Override
        public void channel(Channel chan) {
            channels.add(chan);
        }
    }));

    HttpFetcher fetcher = null;

    try {

        URL url = new URL(Globals.getBackend().getStatusURL() + "/Myth/GetProgramGuide?" + //$NON-NLS-1$
                "StartTime=" + Globals.dateFormat(start) + //$NON-NLS-1$
                "&EndTime=" + Globals.dateFormat(end) + //$NON-NLS-1$
                "&StartChanId=0" + "&NumOfChannels=-1" + "&Details=1" //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        );

        LogUtil.debug("Fetching XML from " + url.toString()); //$NON-NLS-1$

        fetcher = new HttpFetcher(url.toString(), Globals.muxConns);
        InputStream is = fetcher.getInputStream();
        if (is == null)
            throw new IOException(Messages.getString("Guide.0")); //$NON-NLS-1$

        Xml.parse(is, Xml.Encoding.UTF_8, handler);

    } catch (SAXException e) {
        ErrUtil.postErr(this, Messages.getString("Guide.13")); //$NON-NLS-1$
    } catch (IOException e) {
        ErrUtil.postErr(this, e);
    } finally {
        if (fetcher != null)
            try {
                fetcher.endStream();
            } catch (IOException e) {
            }
    }

}