0

背景:

处理从网站检索信息的应用程序。这是我几个月前工作的一个应用程序,当时一切都很好,但我回来进行维护并继续该项目。

我在检索什么:

查看源代码:http ://services.runescape.com/m=news/latest_news.rss

请查看源代码以查看 XML。请注意,每个项目都有类别、链接、pubDate、标题、描述和 guid。只有少数物品有外壳。

问题:

每个标签都以 < tag > 开头并以 </ tag > 结尾,除了以 / > 结尾的附件,这是使阅读过程混乱的原因,但我不知道如何解释它,或者它是否格式正确XML。

问题:

有没有办法处理不以传统的</tag>方式结束的标签?

以下是我到目前为止的所有代码- 如果有人有任何问题,请随时提问。感谢stackoverflow-ers的任何帮助或评论,非常感谢。

我如何处理它:

我使用 SAXParserFactory 和 XML Reader:

            try 
            {
                /** Handling XML */
                SAXParserFactory spf = SAXParserFactory.newInstance();
                SAXParser sp = spf.newSAXParser();
                XMLReader xr = sp.getXMLReader();

                /** Send URL to parse XML Tags */
                URL sourceUrl = new URL("http://services.runescape.com/m=news/latest_news.rss");
                /**
                 * Create handler to handle XML Tags ( extends
                 * DefaultHandler )
                 */
                //MyXMLHandler myXMLHandler = new MyXMLHandler();
                xr.setContentHandler(new MyXMLHandler());
                xr.parse(new InputSource(sourceUrl.openStream()));
            } 
            catch (Exception e) 
            {
                e.printStackTrace();
                //Log.i("EXCEPTION:","HomeActivity.java, line 121 - xml not parsed");
            }
            /** Get result from MyXMLHandler XMLlist Object */
            newsList = MyXMLHandler.xMLList; 

全局变量:

private XMLList newsList;

XMLList 类:

我通过从 XMLList 类的所有 get 方法中检索列表来显示新闻“项目”。

因此,如果有 15 个项目,则应该有 15 个日期、类别、链接等。但是对于附件问题,它会启动附件,但不会结束它们。

所以我得到了 15 个标题,但只有 12 个日期、12 个类别、12 个链接等。如果你在上面的 rss 的源中搜索“enclosure”这个词(查看源:http ://services.runescape.com/m=news /latest_news.rss),您将看到只有 3 个项目有附件。

public class XMLList
{

/** Variables */
private ArrayList<String> title = new ArrayList<String>();
private ArrayList<String>  detail = new ArrayList<String>();
private ArrayList<String> description = new ArrayList<String>();
private ArrayList<String> link = new ArrayList<String>();
private ArrayList<String> date = new ArrayList<String>();
private ArrayList<String> category = new ArrayList<String>();
/**
 * In Setter method default it will return arraylist change that to add
 */

public XMLList()
{
    title.clear();
    detail.clear();
    description.clear();
    link.clear();
    date.clear();
    category.clear();
}
public ArrayList<String> getTitle()
{
    return title;
}
public void setTitle(String name)
{
    this.title.add(name);
}
public ArrayList<String> getDetail()
{
    return detail;
}
public void setDetail(String detail)
{
    this.detail.add(detail);
}
public ArrayList<String> getDescription()
{
    return description;
}
public void setDescription(String description)
{
    this.description.add(description);
}
public void setLink(String link)
{
    this.link.add(link);
}
public ArrayList<String> getLink()
{
    return link;
}
public void setDate(String date)
{
    this.date.add(date);
}
public ArrayList<String> getDate()
{
    return date;
}
public void setCategory(String cat)
{
    this.category.add(cat);
}
public ArrayList<String> getCategory()
{
    return category;
}
}

MyXMLHandler 类:

public class MyXMLHandler extends DefaultHandler
{
public static XMLList xMLList;
Boolean currentElement = false;
String currentValue = null;

Boolean inTitle = false;
Boolean inDescription = false;
Boolean inItem = false;
Boolean inDate = false;
Boolean inLink = false;
Boolean inCategory = false;

StringBuilder buff = null;

public MyXMLHandler()
{
    xMLList = new XMLList();
}
// All methods auto called in this order - start, characters, end
/*
 * Called when an xml tag starts
 * imgView.setImageResource(R.drawable.newImage);
 */
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes)
{
    if(xMLList == null)
    {
        xMLList = new XMLList();
    }
    if(localName.equals("item"))
    {
        inItem = true;
    }
    if (inItem) 
    {
        Log.d("START " + localName,"");
        if (localName.equals("title")) 
        {
            inTitle = true;
            buff = new StringBuilder();
        }
        if (localName.equals("description")) 
        {
            inDescription = true;
            buff = new StringBuilder();
        }
        if (localName.equals("link")) 
        {
            inLink = true;
            buff = new StringBuilder();
        }
        if (localName.equals("pubDate")) 
        {
            inDate = true;
            buff = new StringBuilder();
        }
        if (localName.equals("category")) 
        {
            inCategory = true;
            buff = new StringBuilder();
        }
    }
}
/*
 * Called when an xml tag ends
 */
@Override
public void endElement(String uri, String localName, String qName)throws SAXException
{
    if (inItem && !inTitle && !inDescription && !inLink && !inDate && !inCategory) 
    {
        Log.d("END ITEM", "");
        inItem = false;
    } 
    else if (inTitle) 
    {
        String check = buff.toString().trim();
        Log.d("TITLE:", check);
        Log.d("END " + localName,"");
        xMLList.setTitle(check);
        inTitle = false;
        buff = null;
    }
    else if (inDescription) 
    {
        String check  = buff.toString().trim();
        Log.d("DESC:", check);
        Log.d("END " + localName,"");
        xMLList.setDescription(check);
        inDescription = false;
        buff = null;
    }
    else if (inLink) 
    {
        String check  = buff.toString().trim();
        Log.d("LINK:", check);
        Log.d("END " + localName,"");
        xMLList.setLink(check);
        inLink = false;
        buff = null;
    }
    else if (inDate) 
    {
        String check  = buff.toString().trim();
        Log.d("DATE:", check);
        Log.d("END " + localName,"");
        check = check.substring(0,16);
        xMLList.setDate(check);
        inDate = false;
        buff = null;
    }
    else if(inCategory)
    {
        String check  = buff.toString().trim();
        Log.d("CATEGORY:", check);
        Log.d("END " + localName,"");
        xMLList.setCategory(check);
        inCategory = false;
        buff = null;
    }
}

/*
 * Called to get tag characters
 */
@Override
public void characters(char[] ch, int start, int length)throws SAXException
{
    if (buff != null) 
    {
        for (int i = start; i < start + length; i++) 
        {
            buff.append(ch[i]);
        }
    }

}
}
4

1 回答 1

1

在您的endElement方法中,您有:

    if (inItem && !inTitle && !inDescription && !inLink && !inDate && !inCategory)
    {
        Log.d("END ITEM", "");
        inItem = false;
    }

当解析器命中标签时,此设置inItem为 false 。<enclosure />

inItem仅当您实际遇到结束标签时才应设置为 false </item>,该标签将通过点击“项目”来endElement检测localname

您在 XMLList 类中使用多个列表也很脆弱。您最好使用与您正在阅读的标签匹配的字段创建类并构建该类的对象的单个列表。

于 2013-03-02T19:18:04.573 回答