parsing huge length string using sax parser in android

后端 未结 3 1201
面向向阳花
面向向阳花 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<start+length; i++) {
                strBuilder.append(ch[i]);
            }
        }
    
        if (parsingTitle) {
            if (currentItem != null)
    
                currentItem.setTitle(new String(ch, start, length));
                parsingTitle = false;
    
        }
    
        else if (parsingLink) {
            if (currentItem != null) {
                currentItem.setLink(new String(ch, start, length));
                parsingLink = false;        
        }
        }
        else if (parsingDate) {
            if (currentItem != null) {
    
                currentItem.setDate(new String(ch, start, length));
                parsingDate = false;
    
        }
        }
    
        else if (parsingDescription) {
            if (currentItem != null && strBuilder.toString().contains("(EndEvent)" )) {
    
                       String descriptionTester = strBuilder.toString();
    
                currentItem.setDescription(descriptionTester);
    
                parsingDescription = false;
    
                }
        } 
    } 
    

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

    0 讨论(0)
  • 2021-01-24 11:00

    In sax parser, characters() method parses only maximum of 1024 characters each time. So we need to append the strings until all the characters are parsed.

    I changed the above code as follows

    public void characters(char[] ch, int start, int length)
            throws SAXException
    {
    
    
        Log.d("prabhu","Customer image length in parser......"+length);
        if (currentElement ) {
    
            tempValue = new String(ch,start, length);
    
            if(tempValue.equals(null))
                tempValue = "";
    
    
    
        }
            tempValue = tempValue+new String(ch,start, length);
     }
    
    0 讨论(0)
  • 2021-01-24 11:10

    The output you posted is exactly 1024 characters. This looks like a certain buffer size. How do you get this output? Maybe check that method and / or your CustomerPojoInList.

    I very much believe, that there is some buffer involved that has a maximum of 1024 characters...

    Good luck!

    0 讨论(0)
提交回复
热议问题