SAXParser Android,ArrayList重复元素

时间:2013-08-13 01:31:18

标签: java android arraylist saxparser

我目前正在尝试处理 节点中的元素。为简单起见,我现在只关注 标题 ,但我发现当它解析时,我只是三次获得相同的元素。

http://open.live.bbc.co.uk/weather/feeds/en/2643123/3dayforecast.rss

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

public class XMLHelper extends DefaultHandler {
    private String URL_Main="http://open.live.bbc.co.uk/weather/feeds/en/2643123/3dayforecast.rss";
    String TAG = "XMLHelper";

    Boolean currTag = false;
    String currTagVal = "";     

    public ItemData item = null;
    public ArrayList<ItemData> items = new ArrayList<ItemData>();

    public void get() {
        try {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();
            XMLReader reader = parser.getXMLReader();
            reader.setContentHandler(this);
            InputStream inputStream = new URL(URL_Main).openStream();
            reader.parse(new InputSource(inputStream));
        } catch (Exception e) {
            Log.e(TAG, "Exception: " + e.getMessage());
        }
    }

    // Receives notification of the start of an element

    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {

        Log.i(TAG, "TAG: " + localName);

        currTag = true;
        currTagVal = "";
        if (localName.equals("channel")) {
            item = new ItemData();
        }

    }

    // Receives notification of end of element

    public void endElement(String uri, String localName, String qName)
            throws SAXException {

        currTag = false;

        if (localName.equalsIgnoreCase("title"))
            item.setTitle(currTagVal);


        else if (localName.equalsIgnoreCase("item"))
            items.add(item);


    }

    // Receives notification of character data inside an element 

    public void characters(char[] ch, int start, int length)
            throws SAXException {

        if (currTag) {
            currTagVal = currTagVal + new String(ch, start, length);

            currTag = false;
        }

    }
}

2 个答案:

答案 0 :(得分:0)

您获得相同值三次的原因是因为您在channel方法中存在startElement标记时创建了对象。

    if (localName.equals("channel")) {
        item = new ItemData();
    }

我想你应该在有下面的项目标签时启动对象

    if (localName.equals("item")) { // check for item tag
        item = new ItemData();
    }

答案 1 :(得分:0)

重新修改整个项目,需要3个课程:

1.ItemList 2.XMLHandler扩展了Default处理程序 3.SAXParsing活动

首先整理您的代码

在您的XMLHandler类中,扩展默认处理程序,您的代码应该看起来像

public class MyXMLHandler extends DefaultHandler
{
public static ItemList itemList;
public boolean current = false;
public String currentValue = null;

@Override
public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {
    // TODO Auto-generated method stub

    current = true;

    if (localName.equals("channel"))
    {
        /** Start */ 
        itemList = new ItemList();

    } 
}

@Override
public void endElement(String uri, String localName, String qName)
        throws SAXException {
    // TODO Auto-generated method stub
    current = false;

    if(localName.equals("item"))
    {
        itemList.setItem(currentValue);
    }
    else if(localName.equals("title"))
    {
        itemList.setManufacturer(currentValue);
    }

}

@Override
public void characters(char[] ch, int start, int length)
        throws SAXException {
    // TODO Auto-generated method stub

    if(current)
    {
        currentValue = new String(ch, start, length);
        current=false;
    }
}
} 

ItemList类用于设置,setter和getter方法传递arraylist的值并在SAXParsing活动中检索这些数组列表。

我希望这个解决方案有所帮助。 :d