Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7848617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:16:12+00:00 2026-06-02T18:16:12+00:00

Iam using sax parser to parse the xml file of 3.8 mb. Its loading

  • 0

Iam using sax parser to parse the xml file of 3.8 mb. Its loading time while parsing is almost about 3.5 minutes.

public class sentmsghandler extends DefaultHandler {
public folder folderobj;
public Contact contact;
public Parent parent;
public Student student;
private StringBuilder builder;
Boolean repeat;

public folder getfolder() {
    return folderobj;
}

@Override
public void endElement(String uri, String localName, String qName)
        throws SAXException {
    // TODO Auto-generated method stub
    super.endElement(uri, localName, qName);
    // Log.i("testing parent contact",localName);
    if (this.folderobj != null) {

        String st = builder.toString();
        if (localName.toUpperCase().equalsIgnoreCase(
                BaseFeedParser.SchoolName.toUpperCase())) {
            folderobj.setSchoolName(st);
        } else if (localName.toUpperCase().equalsIgnoreCase(
                BaseFeedParser.ParentCode.toUpperCase())) {
            parent.setParentCode(st);
        } else if (localName.toUpperCase().equalsIgnoreCase(
                BaseFeedParser.FamilyCode.toUpperCase())) {
            if (!repeat) {
                parent.setFamilyCode(st);
            } else {
                student.setFamilyCode(st);
            }
        }

        else if (localName.toUpperCase().equalsIgnoreCase(
                BaseFeedParser.FamilyName.toUpperCase())) {

            if (!repeat) {
                parent.setFamilyName(st);
            } else {
                student.setFamilyName(st);
            }
        }

        else if (localName.toUpperCase().equalsIgnoreCase(
                BaseFeedParser.GivenNames.toUpperCase())) {

            if (!repeat) {
                parent.setGivenNames(st);
            } else {
                student.setGivenNames(st);
            }
        } else if (localName.toUpperCase().equalsIgnoreCase(
                BaseFeedParser.ParentType.toUpperCase())) {
            parent.setParentType(st);
        } else if (localName.toUpperCase().equalsIgnoreCase(
                BaseFeedParser.EmailAddress.toUpperCase())) {
            parent.setEmailAddress(st);
        } else if (localName.toUpperCase().equalsIgnoreCase(
                BaseFeedParser.MobilePhone.toUpperCase())) {
            parent.setMobilePhone(st);
        } else if (localName.toUpperCase().equalsIgnoreCase(
                BaseFeedParser.LandLineNumber.toUpperCase())) {
            parent.setLandLineNumber(st);
        } else if (localName.toUpperCase().equalsIgnoreCase(
                BaseFeedParser.Primary.toUpperCase())) {
            parent.setPrimary(st);
        } else if (localName.toUpperCase().equalsIgnoreCase(
                BaseFeedParser.StudentCode.toUpperCase())) {
            student.setStudentCode(st);
        } else if (localName.toUpperCase().equalsIgnoreCase(
                BaseFeedParser.SchoolYearLevel.toUpperCase())) {
            student.setSchoolYearLevel(st);
        } else if (localName.toUpperCase().equalsIgnoreCase(
                BaseFeedParser.RollClass.toUpperCase())) {
            student.setRollClass(st);
        } else if (localName.toUpperCase().equalsIgnoreCase(
                BaseFeedParser.House.toUpperCase())) {
            student.setHouse(st);
        }

    }
    builder.setLength(0);
}

@Override
public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {
    // TODO Auto-generated method stub
    super.startElement(uri, localName, qName, attributes);

    if (localName.toUpperCase().equalsIgnoreCase(
            BaseFeedParser.Folder.toUpperCase())) {
        folderobj = new folder();
    }

    else if (localName.toUpperCase().equalsIgnoreCase(
            BaseFeedParser.Contact.toUpperCase())) {
        contact = new Contact();
        folderobj.getContact_list().add(contact);
    } else if (localName.toUpperCase().equalsIgnoreCase(
            BaseFeedParser.Parent.toUpperCase())) {
        parent = new Parent();
        repeat = false;
        contact.getParent_list().add(parent);
    } else if (localName.toUpperCase().equalsIgnoreCase(
            BaseFeedParser.Student.toUpperCase())) {
        student = new Student();
        repeat = true;
        parent.getStudent_list().add(student);
    }

}

@Override
public void characters(char[] ch, int start, int length)
        throws SAXException {
    // TODO Auto-generated method stub
    super.characters(ch, start, length);
    builder.append(ch, start, length);
}

@Override
public void startDocument() throws SAXException {
    // TODO Auto-generated method stub
    super.startDocument();
    folderobj = new folder();
    builder = new StringBuilder();
    repeat = false;
}

}

Saxfeedparser.java

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

public class SaxFeedParser extends BaseFeedParser {

    public SaxFeedParser(String feedUrl) {
        super(feedUrl);
        // TODO Auto-generated constructor stub
    }

    public folder parse() throws RuntimeException {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        sentmsghandler handler = null;
        try {
            SAXParser parser = factory.newSAXParser();
            handler = new sentmsghandler();
            parser.parse(this.getInputStream(), handler);

        } catch (Exception e) {
            return null;
        }
        return handler.getfolder();
    }

}

Basefeedparser.java

public abstract class BaseFeedParser implements SentParser {

    // names of the XML tags
    static final String Folder = "Folder";
    static final String SchoolName = "SchoolName";
    static final String Contact = "Contact";
    static final String Parent = "Parent";
    static final String ParentCode = "ParentCode";
    static final String FamilyCode = "FamilyCode";
    static final String FamilyName = "FamilyName";
    static final String GivenNames = "GivenNames";
    static final String ParentType = "ParentType";
    static final String EmailAddress = "EmailAddress";
    static final String MobilePhone = "MobilePhone";
    static final String LandLineNumber = "LandLineNumber";
    static final String Primary = "Primary";
    static final String Student = "Student";
    static final String StudentCode = "StudentCode";
    static final String SchoolYearLevel = "SchoolYearLevel";
    static final String RollClass = "RollClass";
    static final String House = "House";

    //

    final URL feedUrl;

    protected BaseFeedParser(String feedUrl) {
        try {
            this.feedUrl = new URL(feedUrl);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
    }

    protected InputStream getInputStream() {
        try {
            return feedUrl.openConnection().getInputStream();
        } catch (IOException e) {
            return null;
        }
    }
}

Calling the code in activity with

SaxFeedParser sax = new SaxFeedParser("link");// /check parents.xml
folder result = sax.parse();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T18:16:15+00:00Added an answer on June 2, 2026 at 6:16 pm

    One thing you could improve:

    instead of doing

    localName.toUpperCase().equalsIgnoreCase(
                BaseFeedParser.Contact.toUpperCase()
    

    all the time, save BaseFeedParser.Contact as uppercase already, and convert localName only once into uppercase

    localName = localName.toUpperCase();
    if (localName.equals(BaseFeedParser.Folder)) {
        // something
    } else if (localName.equals(BaseFeedParser.Contact)) {
       // something else
    } else if (localName.equals(BaseFeedParser.Parent)) {
      // something here
    }
    

    That should give you a bit of a speed boost.

    If you know that the case does not change you can even leave out the uppercasing completely.

    another technique that might speed up the string comparisons is to use a HashMap

    private static final int CASE_FOLDER = 1;
    private static final int CASE_SCHOOLNAME = 2;
    private static final HashMap<String, Integer> MAP = new HashMap<String, Integer>();
    static {
        MAP.put("FOLDER", CASE_FOLDER);
        MAP.put("SCHOOLNAME", CASE_SCHOOLNAME);
    }
    

    then do

    localName = localName.toUpperCase();
    int match = MAP.get(localName);
    switch (match) {
        case CASE_FOLDER:
            // do something
            break;
     // etc
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using SAX Parser to parse the XML file over the network. While
i parsed my xml file successfully using sax parser.my doubt is how to convert
I am trying to parse an XML file using the SAX interface of libxml2
I am parsing XML Document using SAX Parser. I want to know which is
I am using SAX to parse an XML file I'm pulling from the web.
I am trying to parse XML in Perl using XML::SAX parser . My query
SAXParseException is occuring while iam trying to parse an xml unsing dom parser here
I want to extract attributes from an xml file using Sax Parser I am
I am using SAX Parser to parser XML file on Blackberry platform. Some XML
I am using the SAX Parser for XML Parsing. The problem is for the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.