SAXParser Android, ArrayList repeating elements

蓝咒 提交于 2019-12-13 06:13:55

问题


I am currently just trying to process the elements within the item nodes. I am just focusing on the title at this point for simplicity, but I am finding that when it parses, I am just getting the same element three times.

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

    }
}

回答1:


The reason you are getting the same value thrice is because you are creating the object when there is a channel tag in startElement method.

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

I guess you should initiate the object whenever there is a item tag as below

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



回答2:


Remodify your whole project, you need 3 classes :

1.ItemList 2.XMLHandler extends Default handler 3.SAXParsing activity

Make your code organized first

In your XMLHandler class extend default handler your code should look like

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 class is used to set , setter and getter methods to pass in values of arraylist and retrieve those array lists in the SAXParsing activity.

I hope this solution helps. :D



来源:https://stackoverflow.com/questions/18199289/saxparser-android-arraylist-repeating-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!