【问题标题】:SAXParser Android, ArrayList repeating elementsSAXParser Android,ArrayList 重复元素
【发布时间】:2013-08-13 01:31:18
【问题描述】:

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

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

    }
}

【问题讨论】:

    标签: java android arraylist saxparser


    【解决方案1】:

    三次获得相同值的原因是,当startElement 方法中有channel 标记时,您正在创建对象。

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

    我猜你应该在有如下项目标签时启动对象

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

    【讨论】:

    • 我确实尝试过,但没有返回任何元素,RSS 是这样的。 某事 1


      某事 2
      东西 3
    • 我不确定您面临什么问题。您可以尝试使用“qName”而不是“localName”吗
    • 对不起,试图举一个我正在解析的数据的例子,显然失败了!顶部的链接显示了我要从中解析的内容。使用 qName 没有任何区别。
    【解决方案2】:

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

    1.项目列表 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 类用于设置、setter 和 getter 方法以传入 arraylist 的值并在 SAXParsing 活动中检索这些数组列表。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-03
      • 2013-04-04
      • 1970-01-01
      • 1970-01-01
      • 2016-10-05
      • 1970-01-01
      • 2012-03-03
      • 2017-04-28
      相关资源
      最近更新 更多