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

  • Home
  • SEARCH
  • 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 6995589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:02:57+00:00 2026-05-27T20:02:57+00:00

Im parsing an xml file. My XML handler, my object that holds an Arraylist

  • 0

Im parsing an xml file. My XML handler, my object that holds an Arraylist of arrays, and the main class that runs everything and prints it. The problem is, every time I add an array to my arraylist, it changes the all of the previously added arrays to the same as the current. I thought it was just a static problem but once I took static out of everything it still is doing the same thing. Help please I need to have this done asap.

Here is my handler:

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class MyXMLHandler extends DefaultHandler {

public int counter = 0;
public String[] part = new String[4];
Boolean currentElement = false;
String currentValue = null;
public SitesList sitesList = null; /this used to be static

public SitesList getSitesList() {  //this used to be static
    return sitesList;
}

public void setSitesList(SitesList sitesList) { //this used to be static
    MyXMLHandler handle = new MyXMLHandler(); //thats why the object
    handle.sitesList = sitesList;
}

/**
 * Called when tag starts ( ex:- <name>text</name> -- <name> )
 */
@Override
public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {

    currentElement = true;

    if (localName.equals("string-array")) {
        /** Start */

        String attr = attributes.getValue("name");
        sitesList = new SitesList(attr);
    }

}

/**
 * Called when tag closing ( ex:- <name>text</name> -- </name> )
 */
@Override
public void endElement(String uri, String localName, String qName)
        throws SAXException {
    currentElement = false;

    /** set value */
    if (counter == 4) {
        sitesList.addPart(part);
        counter = 0;
    }
    if (localName.equalsIgnoreCase("item")) {
        part[counter] = currentValue;
        counter++;
    }
    currentValue = "";

}

/**
 * Called to get tag characters ( ex:- <name>text</name> -- to get
 * text Character )
 */
@Override
public void characters(char[] ch, int start, int length)
        throws SAXException {

    if (currentElement) {
        currentValue = new String(ch, start, length);
        currentElement = false;
    }

}

}

Here is my SitesList object

import java.util.ArrayList;

/** Contains getter and setter method for varialbles */
public class SitesList {

/** Variables */
private ArrayList<String[]> part = new ArrayList<String[]>();

/**
 * In Setter method default it will return arraylist change that to add
 */
public SitesList(String c) {
    String[] comp = new String[1];
    comp[0] = c;
    part.add(comp);
    // company name is part(0)[0]
}

public String getCompany() {
    return this.part.get(0)[0];
}

public ArrayList<String[]> getPart() {
    return part;
}

public void addPart(String[] name) {
    part.add(name);
}

public String getName(int i) {
    return this.part.get(i)[0];
}

public String getComp1(int i) {
    return this.part.get(i)[1];
}

public String getComp2(int i) {
    return this.part.get(i)[2];
}

public String getComp3(int i) {
    return this.part.get(i)[3];
}

public int getSize() {
    return this.part.size();
}

}
  • 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-27T20:02:57+00:00Added an answer on May 27, 2026 at 8:02 pm

    You’re reusing part, i.e. you add it multiple times but overwrite its contents. ArrayList is innocent here 🙂

    Change the adding part to this:

    if (counter == 4) {
        sitesList.addPart(part);
        //create a new array
        part = new String[4];
        counter = 0;
    }
    

    Or, as of Java 6:

    if (counter == 4) {
        //add a copy to the list
        sitesList.addPart(Arrays.copyof(part, part.length));       
        counter = 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hi am working with parsing xml file,i am returning ArrayList of class Objects ,
I am parsing through a XML file that's about 12mb big. I need to
I have a problem that I am getting an xml file from raw resource
I am having a problem while parsing XML file,i used newsRSS parser as a
I'm parsing big XML file with XPathExpression selection for some nodes existing at various
I am parsing a XML file in Java using the W3C DOM. I am
I'm parsing an xml file and i've been trying to strip out the whitespace
What is the fastest method of parsing an XML file in C#? I'm using
I got a string from parsing a XML file which looks like this: Fri,
I am using feedparser for parsing from XML file.But I couldn't parse <geo:lat> ,

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.