0

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

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

    }
}
4

2 回答 2

0

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

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

我想只要有如下项目标签,您就应该启动该对象

    if (localName.equals("item")) { // check for item tag
        item = new ItemData();
    }
于 2013-08-13T02:08:29.867 回答
0

重新修改你的整个项目,你需要 3 个类:

1.ItemList 2.XMLHandler 扩展默认处理程序 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 类用于 set 、 setter 和 getter 方法以传入 arraylist 的值并在 SAXParsing 活动中检索这些数组列表。

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

于 2013-08-21T14:30:19.997 回答