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 6343901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:32:06+00:00 2026-05-24T20:32:06+00:00

I am trying to parse a moderately large xml file, store it in an

  • 0

I am trying to parse a moderately large xml file, store it in an ArrayList then display results in an listview. Currently I have got a problem, but cannot find a solution. Firstly here is my sax parser code:

     try {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();

        DefaultHandler handler = new DefaultHandler() {
            boolean DayofWeek = false;
            boolean DateDD = false;             
            String dayofweek = null; 
            String datedd = null;

            public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException  {
                System.out.println("Start Element :" + qName);

                if (qName.equalsIgnoreCase("DayofWeek"))  {
                    DayofWeek = true;
                }
                else if (qName.equalsIgnoreCase("Date-DD"))  {
                    DateDD = true;
                }

                                   // ommited
            }

            public void endElement(String uri, String localName,String qName) throws SAXException  {        

            }

            public void characters(char ch[], int start, int length) throws SAXException  {
                if (DayofWeek) {
                    System.out.println(new String(ch,start,length));
                    dayofweek =  new String(ch, start, length);
                    DayofWeek = false;
                }
                else if (DateDD) {
                    System.out.println(new String(ch,start,length));
                    datedd =  new String(ch, start, length);
                    DateDD = false;
                }
                // ommited
                else if (dayofweek != null) {
                    System.out.println("Do i reach here?");
                    addElement(dayofweek, datedd, datemm, dateyy, time, competition, home, away, venue); //is run
                }
            }
        };
        saxParser.parse(is, handler);

    }  catch (Exception e)  {
        System.out.println("Sax Error");
        Log.w("SaxError", e);
    }
}

private void addElement(String dayofweek, String datedd, String datemm, String dateyy, String time, String competition, String home, String away, String venue)  {
    System.out.println("Add Element");
    AddObjectToList(dayofweek, datedd);
}

// Add one item into the Array List
public void AddObjectToList(String dayofweek, String datedd, String datemm, String dateyy, String time, String competition, String home, String away, String venue) {
    System.out.println("AddObjectToList");
    fixture = new FixtureSupport();
    fixture.setDayOfWeek(dayofweek);
    fixture.setDateDD(datedd);
    itemList.add(fixture);
}

In logcat, this will print all the information correctly, but nothing will go into the AddElement document so nothing appears in the listview. Whereas, if I were to change all the ‘else if’ statements to ‘if’ statements the code will print out in the listview but each XML element will get repeated about 10 or so times (seems random). Whilst it also repeats itself it only prints one of the xml elements at a time and gradually other elements get added to it.

I can’t understand why this is happening, any help please guys!

Thanks

  • 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-05-24T20:32:07+00:00Added an answer on May 24, 2026 at 8:32 pm

    Fix this issue:

            boolean DayofWeek = false;
            String dayofweek = null; 
    

    1) It’s completely confusing

    2) variables should always start with a lower case letter

    3) I have no clue if this affects your code logic

    Try:

            boolean foundDayofWeek = false;
            String dayOfWeekText = null; 
    

    Ref: Java Naming Conventions

    Your characters() method needs to append to a StringBuffer, not all characters may be sent at once, so you have to keep appending then in the endElement() method you call .toString on your buffer.

      // Pseudo
      Stringbuffer yourStringBuffer;
    
      startElement() {
        yourStringBuffer = new Stringbuffer();
      }
    
      public void characters(char[] ch, int start, int length) {
          yourStringBuffer.append(ch, start, start + length);
      }
    
      endElement() {
          // If ....
          String elementText = yourStringBuffer.toString();
      }
    

    Ref: OTT XML Parsing

    Methods should also start with a lowercase letter just for clarity (Class names are uppercase) change:

     AddObjectToList()
    

    to

     addObjectToList();
    

    and see how we go from there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an xml feed at this url Now im trying parse the content,
I'm trying to parse a section of a large file with Java's Scanner library,
I'm trying to parse an xml file from a website. Let's say the website
I have been trying to parse this xml in c# <schema uri=http://blah.com/schema > <itemGroups>
I'm trying to parse an INI file using C++. Any tips on what is
I'm trying to parse objects to XML in Delphi, so I read about calling
I have been trying to parse Java exceptions that appear in a log for
Trying to parse Maplines for an airport. Each airport can have X number of
Trying to parse some XML but apparently this is too much for a lazy
Whilst trying to parse MS Excel file using POI-HSSF v3.2 I am getting IndexOutOfBoundsException.

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.