Java XML parser duplicating elements on list

36 Views Asked by At

I have a SAX2 XML parser and I want to perform specific actions.

  1. Add an object to tileFrames list when frame element starts (as many times as frame in each animation element),
  2. Add String currentTileID with this tileFrames list when animation element ends.
  3. Clear a tileFrames list when animation starts so that the tileAnimations list receive its elements and without ellements dedcated for another animation element.

I want this scenario to perfomr as long as there are no more animation tags in my xml file.

Now I receive list of objects with correct currentTileID but with the same elements like it was the last animation iteration.

Here is what I get when I log:

2023-04-16 14:07:30.538 25714-25714/abc.def.ghi D/tile_animations: TileAnimation{localGID=21, tileFrames=[TileFrame{tileId=42, duration=500}, TileFrame{tileId=43, duration=500}]}
2023-04-16 14:07:30.538 25714-25714/abc.def.ghi D/tile_animations: TileAnimation{localGID=22, tileFrames=[TileFrame{tileId=42, duration=500}, TileFrame{tileId=43, duration=500}]}
2023-04-16 14:07:30.538 25714-25714/abc.def.ghi D/tile_animations: TileAnimation{localGID=43, tileFrames=[TileFrame{tileId=42, duration=500}, TileFrame{tileId=43, duration=500}]}
(...)

Here is the key code (if I cut off too much code I will attach more, I just wanted to simplify things):

TmxHandler.java

@Override
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
    if (localName.equals("animation")) {
        tileFrames.clear();

    } else if (localName.equals("frame")) {
        tileFrames.add(new TileFrame(Long.parseLong(atts.getValue("tileid")), Long.parseLong(atts.getValue("duration"))));

    }
}

@Override
public void endElement(String namespaceURI, String localName, String qName) {
     if (localName.equals("animation")) {
        currentTileSet.tileAnimations.add(new TileAnimation(Long.parseLong(currentTileID), tileFrames));


    } else if (localName.equals("frame")) {

    } 
}

TileAnimation.java

public class TileAnimation {

private long localGID;
private List<TileFrame> tileFrames;

public long getLocalGID() {
    return localGID;
}

public void setLocalGID(long localGID) {
    this.localGID = localGID;
}

public List<TileFrame> getTileFrames() {
    return tileFrames;
}

public void setTileFrames(List<TileFrame> tileFrames) {
    this.tileFrames = tileFrames;
}

public TileAnimation(long localGID, List<TileFrame> tileFrames) {
    this.localGID = localGID;
    this.tileFrames = tileFrames;
}

@Override
public String toString() {
    return "TileAnimation{" +
            "localGID=" + localGID +
            ", tileFrames=" + tileFrames +
            '}';
}

}

And here is my xml

<?xml version="1.0" encoding="UTF-8"?>
<map version="1.10" tiledversion="1.10.0" orientation="orthogonal" renderorder="right-down" width="8" height="12" tilewidth="32" tileheight="32" infinite="0" nextlayerid="11" nextobjectid="1">
 <tileset firstgid="1" name="RPG Nature Tileset" tilewidth="32" tileheight="32" tilecount="180" columns="20">
  <image source="NatureTileset.png" width="641" height="288"/>
  <tile id="21">
   <animation>
    <frame tileid="21" duration="1000"/>
    <frame tileid="1" duration="1000"/>
    <frame tileid="20" duration="1000"/>
   </animation>
  </tile>
  <tile id="22">
   <animation>
    <frame tileid="22" duration="100"/>
    <frame tileid="2" duration="100"/>
   </animation>
  </tile>
  <tile id="43">
   <animation>
    <frame tileid="42" duration="500"/>
    <frame tileid="43" duration="500"/>
   </animation>
  </tile>
 </tileset>
 <tileset firstgid="181" name="32x32_map_tile v3.1 [MARGINLESS]" tilewidth="32" tileheight="32" tilecount="667" columns="23">
  <image source="32x32_map_tile v3.1 [MARGINLESS].png" width="736" height="928"/>
 </tileset>
 <layer id="1" name="grass" width="8" height="12">
  <data encoding="csv">
41,41,41,41,41,41,41,41,
41,41,41,41,41,41,41,41,
41,41,41,41,41,41,41,41,
41,41,41,41,41,41,41,41,
41,41,142,142,142,142,41,41,
41,41,142,41,142,41,41,41,
41,41,41,142,142,41,41,41,
41,41,41,41,41,41,41,41,
41,41,41,41,41,41,41,41,
41,41,41,41,41,41,41,41,
41,41,41,41,41,41,41,41,
41,41,41,41,41,41,41,41
</data>
 </layer>
 <layer id="9" name="anim1" width="8" height="12">
  <data encoding="csv">
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,23,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,44,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0
</data>
 </layer>
 <layer id="10" name="anim2" width="8" height="12">
  <data encoding="csv">
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,22,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0
</data>
 </layer>
</map>

If you need something more just ask.

1

There are 1 best solutions below

1
Cameron On

SCREEN ColorDepth: 24 PixelDepth: 24 Height: 820 Width: 360 AvailHeight: 820 AvailWidth: 360 AvailTop: 0 AvailLeft: 0 Window size: 360x3598 (360x820) PLUGINS Plugins missing EXTENDED WebGL: 076f790265bc8796 Canvas: -151063313 Audio: 8fb5fa54c9d5e0c0 Fonts: 202a809ae5defaa7 Client Rects: da39a3ee5e6b4b0d TIME Zone: Africa/Lagos Local: Sat Apr 15 2023 00:44:44 GMT-0600 (Central Standard Time) System: Sun Apr 16 2023 06:08:19 GMT-0600 (Central Standard Time) IP WEBRTC 104.28.155.240 JAVASCRIPT Enabled FLASH Disabled ACTIVEX Disabled JAVA Disabled COOKIESloxked209.85.255.255 Enabled