parsing huge length string using sax parser in android

主宰稳场 提交于 2019-12-02 07:20:14

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

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!

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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!