for (int y = 0; y < l.getHeight(); y++) { for (int x = 0; x < l.getWidth(); x++) { Tile tile = ((TileLayer)l).getTileAt(x, y); int gid = 0;  if (tile != null) { gid = tile.getGid(); }  w.startElement("tile"); w.writeAttribute("gid", ""+gid); w.endElement(); } }
w.startElement("data"); if (encodeLayerData) { w.writeAttribute("encoding", "base64");  if (compressLayerData) { w.writeAttribute("compression", "gzip"); out = new GZIPOutputStream(baos); } else { out = baos; }  for (int y = 0; y < l.getHeight(); y++) { for (int x = 0; x < l.getWidth(); x++) { Tile tile = ((TileLayer)l).getTileAt(x, y); int gid = 0;  if (tile != null) { gid = tile.getGid(); }  out.write((gid      ) & 0x000000FF); out.write((gid >>  8) & 0x000000FF); out.write((gid >> 16) & 0x000000FF); out.write((gid >> 24) & 0x000000FF); } }  if (compressLayerData) { ((GZIPOutputStream)out).finish(); }  w.writeCDATA(new String(Base64.encode(baos.toByteArray()))); } else { for (int y = 0; y < l.getHeight(); y++) { for (int x = 0; x < l.getWidth(); x++) { Tile tile = ((TileLayer)l).getTileAt(x, y); int gid = 0;  if (tile != null) { gid = tile.getGid(); }  w.startElement("tile"); w.writeAttribute("gid", ""+gid); w.endElement(); } } } w.endElement();
    private void writeMapLayer(MapLayer l, XMLWriter w) throws IOException {        try {            TiledConfiguration conf = TiledConfiguration.getInstance();            ByteArrayOutputStream baos = new ByteArrayOutputStream();            OutputStream out;            boolean encodeLayerData = conf.keyHasValue(                    "tmx.save.encodeLayerData", "1");            boolean compressLayerData = conf.keyHasValue(                    "tmx.save.layerCompression", "1") && encodeLayerData;            Rectangle bounds = l.getBounds();            if (l.getClass() == SelectionLayer.class) {                w.startElement("selection");            } else if(l instanceof ObjectGroup){                writeObjectGroup((ObjectGroup) l, w);            } else {            	w.startElement("layer");            }            w.writeAttribute("name", l.getName());            w.writeAttribute("width", "" + bounds.width);            w.writeAttribute("height", "" + bounds.height);            if (bounds.x != 0) {                w.writeAttribute("xoffset", "" + bounds.x);            }            if (bounds.y != 0) {                w.writeAttribute("yoffset", "" + bounds.y);            }            if (!l.isVisible()) {                w.writeAttribute("visible", "0");            }            if (l.getOpacity() < 1.0f) {                w.writeAttribute("opacity", "" + l.getOpacity());            }            Enumeration keys = l.getProperties();            while (keys.hasMoreElements()) {                String key = (String) keys.nextElement();                w.startElement("property");                w.writeAttribute("name", key);                w.writeAttribute("value", l.getPropertyValue(key));                w.endElement();            }            w.startElement("data");            if (encodeLayerData) {                w.writeAttribute("encoding", "base64");                if (compressLayerData) {                    w.writeAttribute("compression", "gzip");                    out = new GZIPOutputStream(baos);                } else {                    out = baos;                }                for (int y = 0; y < l.getHeight(); y++) {                    for (int x = 0; x < l.getWidth(); x++) {                        Tile tile = ((TileLayer)l).getTileAt(x, y);                        int gid = 0;                        if (tile != null) {                            gid = tile.getGid();                        }                        out.write((gid      ) & 0x000000FF);                        out.write((gid >>  8) & 0x000000FF);                        out.write((gid >> 16) & 0x000000FF);                        out.write((gid >> 24) & 0x000000FF);                    }                }                if (compressLayerData) {                    ((GZIPOutputStream)out).finish();                }                w.writeCDATA(new String(Base64.encode(baos.toByteArray())));            } else {                for (int y = 0; y < l.getHeight(); y++) {                    for (int x = 0; x < l.getWidth(); x++) {                        Tile tile = ((TileLayer)l).getTileAt(x, y);                        int gid = 0;                        if (tile != null) {                            gid = tile.getGid();                        }                        w.startElement("tile");                        w.writeAttribute("gid", ""+gid);                        w.endElement();                    }                }            }            w.endElement();            w.endElement();        } catch (XMLWriterException e) {            e.printStackTrace();        }    }