Android Feed中的RSS Feed解析给出了这个错误=“org.apache.harmony.xml.ExpatParser $ ParseException:在第682行,第0列:文档元素之后的垃圾”

时间:2012-12-05 10:01:29

标签: android eclipse

我的相同代码在其他计算机上运行但是当我运行它我的PC然后它给出了上述错误。我正在使用Eclipse Juno IDE。今天我卸载我的sdk并再次安装,但我的问题是持续的。如何解决我的问题请帮助我。

提前

thanxx

xml解析类的代码

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.example.shareslab.Message;

import android.sax.Element;
import android.sax.EndElementListener;
import android.sax.EndTextElementListener;
import android.sax.RootElement;
import android.util.Xml;

public class BaseFeedParser {

    public static String feedUrlString = "http://www.shareslab.com/rss.xml";

    // names of the XML tags
    static final String RSS = "rss";
    static final String CHANNEL = "channel";
    static final String ITEM = "item";

    static final String PUB_DATE = "pubDate";
    static final String DESCRIPTION = "description";
    static final String LINK = "link";
    static final String TITLE = "title";    
    private final URL feedUrl;

    protected BaseFeedParser(){
        try {
            this.feedUrl = new URL(feedUrlString);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
    }

    protected InputStream getInputStream() {
        try {
            return feedUrl.openConnection().getInputStream();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public List<Message> parse() {
        final Message currentMessage = new Message();
        RootElement root = new RootElement(RSS);
        final List<Message> messages = new ArrayList<Message>();
        Element itemlist = root.getChild(CHANNEL);
        Element item = itemlist.getChild(ITEM);
        item.setEndElementListener(new EndElementListener(){
            @Override
            public void end() {
                messages.add(currentMessage.copy());
            }
        });
        item.getChild(TITLE).setEndTextElementListener(new EndTextElementListener(){
            @Override
            public void end(String body) {
                currentMessage.setTitle(body);
            }
        });
        item.getChild(LINK).setEndTextElementListener(new EndTextElementListener(){
            @Override
            public void end(String body) {
                currentMessage.setLink(body);
            }
        });
        item.getChild(DESCRIPTION).setEndTextElementListener(new EndTextElementListener(){
            @Override
            public void end(String body) {
                currentMessage.setDescription(body);
            }
        });
        item.getChild(PUB_DATE).setEndTextElementListener(new EndTextElementListener(){
            @Override
            public void end(String body) {
                currentMessage.setDate(body);
            }
        });
        try {
            Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return messages;
    }
}

1 个答案:

答案 0 :(得分:0)

如果XML在非Windows机器上运行,则可能是行结尾。在返回值之前,请在getInputStream方法中使用string replacement。例如:

String text = readFileAsString("textfile.txt");
text = text.replace("\n", "").replace("\r", "")