Shivam Shah
Shivam Shah

Reputation: 524

XML not being parsed as expected in Java

I have a xml file as follows

<Moves>
  <Move name="left">
     <Coord X="100" Y="100"/>
     <Coord X="50" Y="100"/>
  </Move>
  <Move name="right">
     <Coord X="10" Y="80"/>
     <Coord X="40" Y="90"/>
  </Move>
<Moves> 

I am parsing it in Java using SAX Parser. the following two methods parse it basically

public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

                if (qName.equalsIgnoreCase("Coord")){
                    X = Integer.parseInt(attributes.getValue("X"));
                    Y = Integer.parseInt(attributes.getValue("Y"));
                } else if (qName.equalsIgnoreCase("Move")) {
                    move_points.clear();
                    move_name = attributes.getValue("name");
                }
            }

            /* If the read element is Move, add a MoveList with the name and if it is
             * a Coord, create a Point with it.
             */
            @Override
            public void endElement(String uri, String localName, String qName) throws SAXException {

                if (qName.equalsIgnoreCase("Coord")){
                    move_points.add(new Points(X, Y));
                } else if (qName.equalsIgnoreCase("Move")) {
                    moves_list.add(new MovesList(move_name, move_points));
                }
          }

I have an ArrayList move_points that stores all the coords read and an Arraylist moves_list that stores the moves name and their coords (which is an arraylist - move_points here)

The problem i am having is that when the document is parsed all the elements that are inside moves_list have the correct name but the entry in move_points or the coord stored are that of the last move in the XML file.

when i am checking what is being entered into moves_list in endElement method after each element Move, it shows the correct coord being entered into moves_list but when the whole document is parsed and i view what is inside moves_list after the root element Moves has been parsed, i am getting moves_list with all the coords of that of the last move.

Please help me out.

PS. moves_list is a public static variable

MovesList Class

public class MovesList {

private ArrayList<Points> move_points;
private String move_name;

public MovesList (String move_name, ArrayList<Points> move_points) {
    this.move_name = move_name;
    this.move_points = move_points;
}

public String getName(){
    return move_name;
}

public ArrayList<Points> getPoints(){
    return move_points;
}

}

Points Class

public class Points extends Point {

private int X;
private int Y;

public Points (int X, int Y) {
    this.X = X;
    this.Y = Y;
}

public Points (Points p) {
    X = p.getIntX();
    Y = p.getIntY();
}

public int getIntX () {
    return X;
}

public int getIntY () {
    return Y;
}

}

Upvotes: 1

Views: 94

Answers (2)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285430

I think that your problem is that you don't create a new move_points object. So this:

} else if (qName.equalsIgnoreCase("Move")) {
  move_points.clear();
  move_name = attributes.getValue("name");
}

Should be this:

} else if (qName.equalsIgnoreCase("Move")) {
  move_points = new ArrayList<Points>(); // note difference
  move_name = attributes.getValue("name");
}

Otherwise each MovesList object will have a move_points variable that refers to the same object.

Upvotes: 2

jtahlborn
jtahlborn

Reputation: 53694

you have a variable named move_points, and when you create a new MovesList, you use a variable named points. is that a typo? also, since you seem to share move_points and clear it when you start a new Move element, i hope you are copying the List when creating the MovesList.

Upvotes: 1

Related Questions