w.startElement("tileproperties");  for (int y = 0; y < l.getHeight(); y++) { for (int x = 0; x < l.getWidth(); x++) { Properties tip = ((TileLayer) l).getTileInstancePropertiesAt(x, y);  if (tip != null && !tip.isEmpty()) { w.startElement("tile");  w.writeAttribute("x", x); w.writeAttribute("y", y);  writeProperties(tip, w);  w.endElement(); } } }  w.endElement();
    private static void writeMapLayer(MapLayer l, XMLWriter w) throws IOException {        Preferences prefs = TiledConfiguration.node("saving");        boolean encodeLayerData =                prefs.getBoolean("encodeLayerData", true);        boolean compressLayerData =                prefs.getBoolean("layerCompression", true) &&                        encodeLayerData;        Rectangle bounds = l.getBounds();        if (l.getClass() == SelectionLayer.class) {            w.startElement("selection");        } else if(l instanceof ObjectGroup){            w.startElement("objectgroup");        } 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());        }        writeProperties(l.getProperties(), w);        if (l instanceof ObjectGroup){            writeObjectGroup((ObjectGroup)l, w);        } else {            w.startElement("data");            if (encodeLayerData) {                ByteArrayOutputStream baos = new ByteArrayOutputStream();                OutputStream out;                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       & LAST_BYTE);                        out.write(gid >> 8  & LAST_BYTE);                        out.write(gid >> 16 & LAST_BYTE);                        out.write(gid >> 24 & LAST_BYTE);                    }                }                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();    }