xml – How to get an image out of a tag?

Question:

I can’t figure out how to implement the sax parser so that pictures are also loaded into the feed.

Parser class:

package app.kray.anddev;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import android.sax.Element;
import android.sax.EndElementListener;
import android.sax.EndTextElementListener;
import android.sax.RootElement;
import android.util.Xml;

public class NewParser {

    protected InputStream getInputStream() {
        URL feedUrl = null;
        try {
            feedUrl = new URL("http://anddev.ru/feed");
        } catch (MalformedURLException e1) {
            feedUrl = null;
        }
        try {
            return feedUrl.openConnection().getInputStream();
        } catch (IOException e) {
            return null;
        }
    }

    public ArrayList<PostItem> parse() {
        final PostItem currentPost = new PostItem();
        final ArrayList<PostItem> messages = new ArrayList<PostItem>();
        RootElement root = new RootElement("rss");
        Element channel = root.getChild("channel");
        Element item = channel.getChild("item");
        item.setEndElementListener(new EndElementListener() {
            public void end() {
                messages.add(currentPost.copy());
            }
        });
        item.getChild("title").setEndTextElementListener(
                new EndTextElementListener() {
                    public void end(String body) {
                        currentPost.title = body;
                    }
                });
        item.getChild("link").setEndTextElementListener(
                new EndTextElementListener() {
                    public void end(String body) {
                        currentPost.link = body;
                    }
                });
        item.getChild("description").setEndTextElementListener(
                new EndTextElementListener() {
                    public void end(String body) {
                        currentPost.description = body;
                    }
                });
        item.getChild("pubDate").setEndTextElementListener(
                new EndTextElementListener() {
                    public void end(String body) {
                        currentPost.setDate(body);
                    }
                });
        try {
            Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8,
                    root.getContentHandler());
        } catch (Exception e) {
            return null;
        }
        return messages;
    }
}

jasoup also jasoup to use jasoup to pull out links to pictures and load them into the imageView , I have little experience in java, but I really want to figure it out. Thanks in advance.

Answer:

first part – sax parser thrashes all XML, breaking into records ( <item></item> )

the second part – with the help of JSoup you parse the values, the bare HTML that SAX Parser collects for you. JSoup can use strings and return structures.

there are a lot of examples on the official website

if you want complete comfort, you can also use JAXB by turning each post in the feed into a java object (POJO):

For example:

class SiteItem:
    private String title;
    private String link;
    private String comments;
    ...
Scroll to Top