Skip to content Skip to sidebar Skip to footer

Parsing Huge Length String Using Sax Parser In Android

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

Solution 1:

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

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

Solution 2:

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!

Solution 3:

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
publicvoidstartElement(String uri, String localName, String qName, Attributes attributes) 
        throws SAXException {

    strBuilder =  new StringBuilder();

    if ("item".equals(qName)) {
        currentItem = new RssItem();
    } elseif ("title".equals(qName)) {
        parsingTitle = true;
    } elseif ("link".equals(qName)) {
        parsingLink = true;
    }
     elseif ("pubDate".equals(qName)) {
            parsingDate = true;
        }
     elseif ("description".equals(qName)) {
            strBuilder =  new StringBuilder(); //reset the strBuilder variable to null
            parsingDescription = true;
        }
}


@Override
publicvoidendElement(String uri, String localName, String qName) throws SAXException {

    String descriptionTester = strBuilder.toString();

    if ("item".equals(qName)) {
        rssItems.add(currentItem);
        currentItem = null;
    } elseif ("title".equals(qName)) {
        parsingTitle = false;
    } elseif ("link".equals(qName)) {
        parsingLink = false;
    }
    elseif ("pubDate".equals(qName)) {
        parsingDate = false;
    }
    //else //  currentItem.setDescription(descriptionTester);elseif ("description".equals(qName) && descriptionTester.contains("(EndEvent)")) {
        parsingDescription = false;
    }
}


@Override
publicvoidcharacters(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;

    }

    elseif (parsingLink) {
        if (currentItem != null) {
            currentItem.setLink(new String(ch, start, length));
            parsingLink = false;        
    }
    }
    elseif (parsingDate) {
        if (currentItem != null) {

            currentItem.setDate(new String(ch, start, length));
            parsingDate = false;

    }
    }

    elseif (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!

Post a Comment for "Parsing Huge Length String Using Sax Parser In Android"