parsing huge length string using sax parser in android

后端 未结 3 1202
面向向阳花
面向向阳花 2021-01-24 10:51

I parsing the xml using sax parser in android. My xml structure is as given below

   
        
              

        
3条回答
  •  故里飘歌
    2021-01-24 10:57

    first time post. Updated answer with something that might help others. I hope it is not too specific to my particular problem. I am parsing an RSS feed that I create myself with a really long description but the other tags you are interested in, i.e. feed title, date and URL are always short. The description contains information about social events. Within the description, I use tags that I later parse to give me information about the event such as event date (different from RSS pubDate), (Location), (ticketDetails), (Phone), etc, you get the idea.

    A good way to handle this is with a slight modification of the answer in this post. I added tags to the description for (Event) and (EndEvent) and I keep appending to my String Builder until I get "(EndEvent)". That way i know i have the full string. It might not work for your situation if you dont control the feed unless you know there is always a certain string at the end of your RSS description.

    Posting in case this (cough, hack) helps anyone. Code is as follows:

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) 
            throws SAXException {
    
        strBuilder =  new StringBuilder();
    
        if ("item".equals(qName)) {
            currentItem = new RssItem();
        } else if ("title".equals(qName)) {
            parsingTitle = true;
        } else if ("link".equals(qName)) {
            parsingLink = true;
        }
         else if ("pubDate".equals(qName)) {
                parsingDate = true;
            }
         else if ("description".equals(qName)) {
                strBuilder =  new StringBuilder(); //reset the strBuilder variable to null
                parsingDescription = true;
            }
    }
    
    
    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
    
        String descriptionTester = strBuilder.toString();
    
        if ("item".equals(qName)) {
            rssItems.add(currentItem);
            currentItem = null;
        } else if ("title".equals(qName)) {
            parsingTitle = false;
        } else if ("link".equals(qName)) {
            parsingLink = false;
        }
        else if ("pubDate".equals(qName)) {
            parsingDate = false;
        }
        //else 
        //  currentItem.setDescription(descriptionTester);
        else if ("description".equals(qName) && descriptionTester.contains("(EndEvent)")) {
            parsingDescription = false;
        }
    }
    
    
    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
    
        if (strBuilder != null) {
            for (int i=start; i

    As I said, hope that helps someone as I was stumped on this for a while!

提交回复
热议问题