Example usage for android.sax RootElement RootElement

List of usage examples for android.sax RootElement RootElement

Introduction

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

Prototype

public RootElement(String localName) 

Source Link

Document

Constructs a new root element with the given name.

Usage

From source file:com.byteridge.bookcircle.utils.ResponseParser.java

public static Response parse(InputStream inputStream) throws IOException, SAXException {
    final Response response = new Response();
    RootElement root = new RootElement("GoodreadsResponse");
    response.set_Book(Book.appendSingletonListener(root, 0));
    response.set_Request(Request.appendSingletonListener(root));
    response.set_User(User.appendSingletonListener(root, 0));
    response.set_Shelves(Shelves.appendSingletonListener(root, 0));
    response.set_Reviews(Reviews.appendSingletonListener(root, 0));
    response.set_Review(Review.appendSingletonListener(root, 0));
    try {//from ww w  .jav  a2 s  .  c om
        Xml.parse(inputStream, Xml.Encoding.UTF_8, root.getContentHandler());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return response;
}

From source file:com.byteridge.bookcircle.framework.listener.ResponseParser.java

public static Response parse(InputStream inputStream) throws IOException, SAXException {
    final Response response = new Response();

    RootElement root = new RootElement("GoodreadsResponse");

    response.set_Book(Book.appendSingletonListener(root, 0));
    response.set_Request(Request.appendSingletonListener(root));
    response.set_User(User.appendSingletonListener(root, 0));
    response.set_Shelves(Shelves.appendSingletonListener(root, 0));
    response.set_Reviews(Reviews.appendSingletonListener(root, 0));

    response.set_Review(Review.appendSingletonListener(root, 0));

    try {//w  w w .  j a va2s .c o  m
        Xml.parse(inputStream, Xml.Encoding.UTF_8, root.getContentHandler());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return response;
}

From source file:com.onesadjam.yagrac.xml.ResponseParser.java

public static Response parse(InputStream inputStream) throws IOException, SAXException {
    final Response response = new Response();

    RootElement root = new RootElement("GoodreadsResponse");

    response.set_Book(Book.appendSingletonListener(root, 0));
    response.set_Request(Request.appendSingletonListener(root));
    response.set_User(User.appendSingletonListener(root, 0));
    response.set_Shelves(Shelves.appendSingletonListener(root, 0));
    response.set_Reviews(Reviews.appendSingletonListener(root, 0));
    response.set_Search(Search.appendSingletonListener(root, 0));
    response.set_Followers(Followers.appendSingletonListener(root, 0));
    response.set_Friends(Friends.appendSingletonListener(root, 0));
    response.set_Following(Following.appendSingletonListener(root, 0));
    response.set_Updates(Update.appendArrayListener(root, 0));
    response.set_Review(Review.appendSingletonListener(root, 0));
    response.set_Author(Author.appendSingletonListener(root, 0));
    response.set_Comments(Comments.appendSingletonListener(root, 0));
    response.set_Events(Event.appendArrayListener(root, 0));

    try {//from   w  ww.  j ava  2s .  c om
        Xml.parse(inputStream, Xml.Encoding.UTF_8, root.getContentHandler());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return response;
}

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 w  w.  jav  a 2s  .  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();
    }
}