not able to add data while parsing xml data using SAXParser

144 views Asked by At

I am tryin to get rss feed using xml parsing and I am getting problem while parsing xml data using SAXParser

I have tried something like In my MainActivity.java

try{


        /****** Creating a new instance of the SAX parser ****************/
        SAXParserFactory saxPF = SAXParserFactory.newInstance();
        SAXParser saxParser = saxPF.newSAXParser();
        XMLReader xmlReader = saxParser.getXMLReader();

        URL url = new URL("http://ibnlive.in.com/ibnrss/rss/world/world.xml");

        myXMLHandler = new FeedsXMLHandler();
        xmlReader.setContentHandler(myXMLHandler);
        xmlReader.parse(new InputSource(url.openStream()));
    }catch (Exception e) {
        e.printStackTrace();
    }

I am retriving data from my handler using

ArrayList<FeedsItems> feedsData = myXMLHandler.getXMLData();
Log.v("size",Integer.toString(feedsData.size()));

Here its showing size as zero.

My FeedsXMLHandler.java

private ArrayList<FeedsItems> dataArray = new ArrayList<FeedsItems>();
private FeedsItems data = null;

@Override
    public void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException 
    {
        elementValue = "";
        elementOn = true;

        if (localName.equalsIgnoreCase("rss")) 
        {
            //  data = new FeedsItems();
        } 

        else if (localName.equalsIgnoreCase("item"))
        {
            data = new FeedsItems();
            Log.v("Item","I am in item block");
        }

        else if (localName.equalsIgnoreCase("description")) 
        {
            bufferDesc = new StringBuilder();
            elementOn = true;
        }

        else if (localName.equalsIgnoreCase("title")) 
        {
            bufferTitle = new StringBuilder();
            elementOn = true;
        }
        else if(localName.equalsIgnoreCase("link")) 
        {
            bufferLink = new StringBuilder();
            elementOn = true;

        }
    }

    /*********** Method will be called when the tags of the XML end **************/
    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {

        elementOn = false;

        /*** Sets the values after retrieving the values from the XML tags ******/ 
        if (localName.equalsIgnoreCase("title"))
        {
            elementOn = false;

            data.setTitle(bufferTitle.toString());
            bufferTitle.delete(0,bufferTitle.length());
        }   

        else if (localName.equalsIgnoreCase("link")){
            elementOn = false;

            data.setFeedsUrl(bufferLink.toString());
            bufferLink.delete(0,bufferLink.length());
        }

        else if (localName.equalsIgnoreCase("description")){
            elementOn = false;

            data.setDescription(bufferDesc.toString());
            bufferDesc.delete(0, bufferDesc.length());

        }
        else if (localName.equalsIgnoreCase("item")){
            dataArray.add(data);
        }


    }

I am not able to understand whats wrong in it. I have already implemented xml parsing using SAXParser in this method. Any help or suggestion will be welcome

0

There are 0 answers