1

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

提前谢谢

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;
    }
}
4

1 回答 1

0

如果 XML 在非 Windows 机器上工作,它可能是行尾。在返回值之前在方法中使用字符串替换。getInputStream例如:

String text = readFileAsString("textfile.txt");
text = text.replace("\n", "").replace("\r", "")
于 2013-01-18T22:45:07.027 回答